TimeZoneService Class
Provides timezone information and conversion services based on the user's browser timezone.
public sealed class TimeZoneService : IAsyncDisposableRemarks
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
| Name | Description |
|---|---|
| TimeZoneService(IJSRuntime) | Initializes a new instance of the TimeZoneService class. |
TimeZoneService(IJSRuntime)
TimeZoneService.csInitializes a new instance of the TimeZoneService class.
public TimeZoneService(IJSRuntime jsRuntime)Parameters
jsRuntimeIJSRuntime- The JavaScript runtime to use for interoperability.
Methods
| Name | Description |
|---|---|
| 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.csGetLocalDateTimeAsync(DateTimeOffset)
TimeZoneService.csConverts 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
dateTimeDateTimeOffset- 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.csGets the user's offset from UTC at the current instant.
Returns
ValueTask<TimeSpan> A task that represents the asynchronous operation. The task result contains the timezone offset.
GetOffsetAsync(DateTimeOffset)
TimeZoneService.csGets 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
instantDateTimeOffset- 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.csGets 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.csConverts 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
dateTimeDateTime- 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.