TimeZoneService Class

Provides timezone information and conversion services based on the user's browser timezone.

public sealed class TimeZoneService : IAsyncDisposable

Remarks

This service retrieves the user's timezone from the browser and provides methods to convert between UTC and local times. To use this service, register it in your dependency injection container using TimeZoneServiceExtensions.AddTimeZoneServices.

The offset of a timezone is not a constant: it changes at daylight saving transitions. Conversions therefore take the instant into account rather than applying a single cached offset to every value.

Example

Register the service in Program.cs: builder.Services.AddTimeZoneServices(); Use it in a component:

@inject TimeZoneService TimeZoneService

<button @onclick="ConvertToLocalTime">Show the local time</button>
<p>@localTime</p>

@code {
    private DateTimeOffset? localTime;

    private async Task ConvertToLocalTime()
    {
        var utcTime = DateTimeOffset.UtcNow;
        localTime = await TimeZoneService.GetLocalDateTimeAsync(utcTime);
    }
}
Implements

Constructors

NameDescription
TimeZoneService(IJSRuntime)Initializes a new instance of the TimeZoneService class.

TimeZoneService(IJSRuntime)

TimeZoneService.cs

Initializes a new instance of the TimeZoneService class.

public TimeZoneService(IJSRuntime jsRuntime)

Parameters

jsRuntime IJSRuntime
The JavaScript runtime to use for interoperability.

Methods

NameDescription
DisposeAsync()Disposes the service and releases all resources.
GetLocalDateTimeAsync(DateTimeOffset)Converts a DateTimeOffset to the user's local timezone.
GetOffsetAsync()Gets the user's offset from UTC at the current instant.
GetOffsetAsync(DateTimeOffset)Gets the user's offset from UTC at the given instant.
GetTimeZoneAsync()Gets the user's timezone, or null when the browser reports an identifier this runtime does not know.
GetUtcDateTimeAsync(DateTime)Converts a DateTime from the user's local timezone to UTC.

DisposeAsync()

TimeZoneService.cs

Disposes the service and releases all resources.

net10.0

public ValueTask DisposeAsync()

net11.0

public ValueTask DisposeAsync()

Returns

ValueTask A task that represents the asynchronous disposal operation.

GetLocalDateTimeAsync(DateTimeOffset)

TimeZoneService.cs

Converts a DateTimeOffset to the user's local timezone.

net10.0

public ValueTask<DateTimeOffset> GetLocalDateTimeAsync(DateTimeOffset dateTime)

net11.0

public ValueTask<DateTimeOffset> GetLocalDateTimeAsync(DateTimeOffset dateTime)

Parameters

dateTime DateTimeOffset
The date and time to convert.

Returns

ValueTask<DateTimeOffset> A task that represents the asynchronous operation. The task result contains the converted date and time.

GetOffsetAsync overloads

GetOffsetAsync()

TimeZoneService.cs

Gets the user's offset from UTC at the current instant.

public ValueTask<TimeSpan> GetOffsetAsync()
Returns

ValueTask<TimeSpan> A task that represents the asynchronous operation. The task result contains the timezone offset.

GetOffsetAsync(DateTimeOffset)

TimeZoneService.cs

Gets the user's offset from UTC at the given instant.

net10.0

public ValueTask<TimeSpan> GetOffsetAsync(DateTimeOffset instant)

net11.0

public ValueTask<TimeSpan> GetOffsetAsync(DateTimeOffset instant)
Parameters
instant DateTimeOffset
The instant at which the offset is evaluated. Daylight saving time makes the offset depend on it.
Returns

ValueTask<TimeSpan> A task that represents the asynchronous operation. The task result contains the timezone offset.

GetTimeZoneAsync()

TimeZoneService.cs

Gets the user's timezone, or null when the browser reports an identifier this runtime does not know.

net10.0

public ValueTask<TimeZoneInfo?> GetTimeZoneAsync()

net11.0

public ValueTask<TimeZoneInfo?> GetTimeZoneAsync()

Returns

ValueTask<TimeZoneInfo?> A task that represents the asynchronous operation. The task result contains the user's timezone.

GetUtcDateTimeAsync(DateTime)

TimeZoneService.cs

Converts a DateTime from the user's local timezone to UTC.

net10.0

public ValueTask<DateTimeOffset> GetUtcDateTimeAsync(DateTime dateTime)

net11.0

public ValueTask<DateTimeOffset> GetUtcDateTimeAsync(DateTime dateTime)

Parameters

dateTime DateTime
The local date and time to convert. If the value is already UTC, it is returned as-is.

Returns

ValueTask<DateTimeOffset> A task that represents the asynchronous operation. The task result contains the UTC date and time.