Class DateTimeExtensions
The DateTime extension class
public static class DateTimeExtensions
- Inheritance
-
DateTimeExtensions
- Inherited Members
Methods
AddTimeSpan(in DateTime, TimeZoneInfo, in TimeSpan)
Adds the provided time span to the DateTime in the provided time zone. The methods converts the given datetime into UTC, adds the timespan, and converts it back to provided time zone to return the value. This keeps the addition invalid-time-safe for DateTimes in time zones that support time zone adjustments (e.g. Daylight Savings).
public static DateTime AddTimeSpan(this in DateTime dateTime, TimeZoneInfo timeZoneInfo, in TimeSpan timeSpan)
Parameters
dateTime
DateTimeThe DateTime value in the given time zone.
timeZoneInfo
TimeZoneInfoThe timezone for the provided datetime
timeSpan
TimeSpanTime span to add to the given datetime
Returns
- DateTime
DateTimeOffset value
Equals(in DateTime, in DateTime, in TimeSpan)
An extension method to check equality between two datetimes considering a precision.
public static bool Equals(this in DateTime dt1, in DateTime dt2, in TimeSpan precision)
Parameters
dt1
DateTimeThe first datetime to compare.
dt2
DateTimeThe second datetime to compare with.
precision
TimeSpanThe precision to consinder while comparing the equality.
Returns
- bool
True if the gievn date time equals to current date time upon considering the threshold, otheerwise, false.
Equals(DateTime?, DateTime?, in Func<DateTime, DateTime>)
An extension method to check equality between two datetimes considering a rounder function to regulate precision.
public static bool Equals(this DateTime? dt1, DateTime? dt2, in Func<DateTime, DateTime> rounder)
Parameters
dt1
DateTime?The first datetime to compare.
dt2
DateTime?The second datetime to compare with.
rounder
Func<DateTime, DateTime>Applies the desired precision.
Returns
- bool
True if the gievn date time equals to current date time upon considering the threshold, otheerwise, false.
TicksSinceNow(in DateTime)
Returns the number of ticks since DateTime.UtcNow for the given DateTime.
public static long TicksSinceNow(this in DateTime dateTime)
Parameters
Returns
ToDateTimeOffset(in DateTime, TimeZoneInfo)
Converts DateTime value to a DateTimeOffset value in the given timezone offset. This method does not convert the given DateTime into the Timezone. The provided DateTime must already be in said timezone. Main purpose of this method is to handle invalid times due to timezone adjustments, such as daylight savings.
public static DateTimeOffset ToDateTimeOffset(this in DateTime dateTime, TimeZoneInfo timeZoneInfo)
Parameters
dateTime
DateTimeThe DateTime value in the given time zone.
timeZoneInfo
TimeZoneInfoThe timezone for the DateTimeOffset
Returns
- DateTimeOffset
DateTimeOffset value
ToDateTimeOffsetFromUtc(in DateTime, TimeZoneInfo)
Converts UTC DateTime value to a DateTimeOffset value in the given timezone offset. Unlike ToDateTimeOffset(in DateTime, TimeZoneInfo), this method takes in a UTC Datetime and performs the conversion to the given timezone.
public static DateTimeOffset ToDateTimeOffsetFromUtc(this in DateTime utc, TimeZoneInfo timeZoneInfo)
Parameters
utc
DateTimeThe DateTime value in UTC.
timeZoneInfo
TimeZoneInfoThe timezone for the DateTimeOffset
Returns
- DateTimeOffset
DateTimeOffset value