Class DateTimeExtensions
- Namespace
- System
- Assembly
- Arc4u.dll
Provides a set of static methods extending the DateTime class.
public static class DateTimeExtensions
- Inheritance
-
DateTimeExtensions
- Inherited Members
Methods
AsLocalTime(DateTime, TimeProvider)
Converts a UTC value to the LocalTimeZone of timeProvider.
public static DateTime AsLocalTime(this DateTime value, TimeProvider timeProvider)
Parameters
valueDateTimeThe value. A value whose Kind is not Utc is considered to be already in the time zone of
timeProviderand is returned unchanged.timeProviderTimeProviderThe time provider, typically a ZonedTimeProvider.
Returns
- DateTime
The wall-clock time in the time zone of
timeProvider. A converted value has a Kind of Unspecified: Local would mean the time zone of the machine.
Examples
@inject TimeProvider Clock
<td>@order.CreatedUtc.AsLocalTime(Clock).ToString("g")</td>
Exceptions
- ArgumentNullException
timeProvideris null.
AsUniversalTime(DateTime, TimeProvider)
Converts a value to Coordinated Universal Time (UTC).
public static DateTime AsUniversalTime(this DateTime value, TimeProvider timeProvider)
Parameters
valueDateTimeThe value:
<ul><li><xref href="System.DateTimeKind.Utc" data-throw-if-not-resolved="false"></xref>: returned unchanged.</li><li><xref href="System.DateTimeKind.Local" data-throw-if-not-resolved="false"></xref>: a time of the machine (for example deserialized from a JSON offset), converted with <xref href="System.DateTime.ToUniversalTime" data-throw-if-not-resolved="false"></xref>.</li><li><xref href="System.DateTimeKind.Unspecified" data-throw-if-not-resolved="false"></xref>: a wall-clock time in the time zone of <code class="paramref">timeProvider</code> (for example typed in a form). A time skipped when the clocks go forward is moved forward by the gap (02:30 becomes 03:30); a time that happens twice when the clocks go back is the first of the two.</li></ul>timeProviderTimeProviderThe time provider, typically a ZonedTimeProvider.
Returns
Exceptions
- ArgumentNullException
timeProvideris null.
SubtractWithDaylightTime(DateTime, DateTime, TimeProvider)
Gets the elapsed time between this instance and value, taking the changes of the daylight saving time into account.
Both values are converted to UTC as AsUniversalTime(DateTime, TimeProvider) does.
public static TimeSpan SubtractWithDaylightTime(this DateTime instance, DateTime value, TimeProvider timeProvider)
Parameters
instanceDateTimeThe first date and time.
valueDateTimeThe second date and time.
timeProviderTimeProviderThe time provider, typically a ZonedTimeProvider.
Returns
- TimeSpan
The absolute duration between the two values.
Exceptions
- ArgumentNullException
timeProvideris null.
ToLocalTime(DateTimeOffset, TimeProvider)
Converts an instant to the LocalTimeZone of timeProvider, keeping its offset.
public static DateTimeOffset ToLocalTime(this DateTimeOffset value, TimeProvider timeProvider)
Parameters
valueDateTimeOffsetThe instant.
timeProviderTimeProviderThe time provider, typically a ZonedTimeProvider.
Returns
- DateTimeOffset
The same instant, with the offset of the time zone of
timeProvider.
Exceptions
- ArgumentNullException
timeProvideris null.