Table of Contents

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

value DateTime

The value. A value whose Kind is not Utc is considered to be already in the time zone of timeProvider and is returned unchanged.

timeProvider TimeProvider

The 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

timeProvider is null.

AsUniversalTime(DateTime, TimeProvider)

Converts a value to Coordinated Universal Time (UTC).

public static DateTime AsUniversalTime(this DateTime value, TimeProvider timeProvider)

Parameters

value DateTime

The 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>
timeProvider TimeProvider

The time provider, typically a ZonedTimeProvider.

Returns

DateTime

A DateTime whose Kind is Utc.

Exceptions

ArgumentNullException

timeProvider is 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

instance DateTime

The first date and time.

value DateTime

The second date and time.

timeProvider TimeProvider

The time provider, typically a ZonedTimeProvider.

Returns

TimeSpan

The absolute duration between the two values.

Exceptions

ArgumentNullException

timeProvider is 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

value DateTimeOffset

The instant.

timeProvider TimeProvider

The time provider, typically a ZonedTimeProvider.

Returns

DateTimeOffset

The same instant, with the offset of the time zone of timeProvider.

Exceptions

ArgumentNullException

timeProvider is null.