DateSpan Struct
Definition
public readonly struct DateSpan : IEquatable<DateSpan>
- Implements
Examples
The following example demonstrates how to calculate the time span between two dates using DateSpan. It shows constructing a span, accessing years/months/days, parsing ISO 8601 date strings, and creating a span that defaults the end to today.
using System;
using Cuemon;
namespace MyApp.Time;
public class DateSpanExample
{
public void Demonstrate()
{
// Create a DateSpan between two dates
var start = new DateTime(2020, 1, 1);
var end = new DateTime(2025, 6, 15);
var span = new DateSpan(start, end);
Console.WriteLine($"Years: {span.Years}"); // 5
Console.WriteLine($"Months: {span.Months}"); // 65
Console.WriteLine($"Days: {span.Days}"); // 1991
Console.WriteLine($"Total days: {span.TotalDays:F1}"); // 1991.0
// Parse from ISO 8601 strings
var parsed = DateSpan.Parse("2020-01-01", "2025-06-15");
Console.WriteLine(parsed.Years); // 5
// Single DateSpan defaults the end to DateTime.Today
var fromPast = new DateSpan(new DateTime(2023, 1, 1));
Console.WriteLine($"Days since 2023-01-01: {fromPast.TotalDays:F0}");
}
}
Constructors
| Name | Description |
|---|---|
| DateSpan(DateTime) | Initializes a new instance of the DateSpan structure with a default DateTime value set to Today. |
| DateSpan(DateTime, DateTime) | Initializes a new instance of the DateSpan structure with a default Calendar value from the InvariantCulture class. |
| DateSpan(DateTime, DateTime, Calendar) | Initializes a new instance of the DateSpan structure. |
Properties
| Name | Description |
|---|---|
| Days | Gets the number of days represented by the current DateSpan structure. |
| Hours | Gets the number of hours represented by the current DateSpan structure. |
| Milliseconds | Gets the number of milliseconds represented by the current DateSpan structure. |
| Minutes | Gets the number of minutes represented by the current DateSpan structure. |
| Months | Gets the number of months represented by the current DateSpan structure. |
| Seconds | Gets the number of seconds represented by the current DateSpan structure. |
| Ticks | Gets the number of ticks represented by the current DateSpan structure. |
| TotalDays | Gets the total number of days represented by the current DateSpan structure. |
| TotalHours | Gets the total number of hours represented by the current DateSpan structure. |
| TotalMilliseconds | Gets the total number of milliseconds represented by the current DateSpan structure. |
| TotalMinutes | Gets the total number of minutes represented by the current DateSpan structure. |
| TotalMonths | Gets the total number of months represented by the current DateSpan structure. |
| TotalSeconds | Gets the total number of seconds represented by the current DateSpan structure. |
| TotalYears | Gets the total number of years represented by the current DateSpan structure. |
| Years | Gets the number of years represented by the current DateSpan structure. |
Methods
| Name | Description |
|---|---|
| Equals(DateSpan) | Indicates whether the current object is equal to another object of the same type. |
| Equals(object) | Determines whether the specified object is equal to this instance. |
| GetHashCode() | Returns a hash code for this instance. |
| GetWeeks() | Calculates the number of weeks represented by the current DateSpan structure. |
| Parse(string) | Constructs a new DateSpan object from a date and time interval specified in a string. |
| Parse(string, string) | Constructs a new DateSpan object from a date and time interval specified in a string. |
| Parse(string, string, CultureInfo) | Constructs a new DateSpan object from a date and time interval specified in a string. |
| ToString() | Converts the value of the current DateSpan object to its equivalent string representation. |
Operators
| Name | Description |
|---|---|
| operator ==(DateSpan, DateSpan) | Indicates whether two DateSpan instances are equal. |
| operator !=(DateSpan, DateSpan) | Indicates whether two DateSpan instances are not equal. |