TailoredApps.Shared.DateTime
2.1.0
See the version list below for details.
dotnet add package TailoredApps.Shared.DateTime --version 2.1.0
NuGet\Install-Package TailoredApps.Shared.DateTime -Version 2.1.0
<PackageReference Include="TailoredApps.Shared.DateTime" Version="2.1.0" />
<PackageVersion Include="TailoredApps.Shared.DateTime" Version="2.1.0" />
<PackageReference Include="TailoredApps.Shared.DateTime" />
paket add TailoredApps.Shared.DateTime --version 2.1.0
#r "nuget: TailoredApps.Shared.DateTime, 2.1.0"
#:package TailoredApps.Shared.DateTime@2.1.0
#addin nuget:?package=TailoredApps.Shared.DateTime&version=2.1.0
#tool nuget:?package=TailoredApps.Shared.DateTime&version=2.1.0
TailoredApps.Shared.DateTime
IDateTimeProvider gives application code the current date and time as ready-to-use values (DateTime,
DateTimeOffset, TimeSpan) from a clock that can be replaced in tests. The clock is System.TimeProvider,
so code that injects IDateTimeProvider and code that injects TimeProvider (timers, delays) read the same time.
Installation
dotnet add package TailoredApps.Shared.DateTime
Registration
using TailoredApps.Shared.DateTime;
builder.Services.AddDateTimeProvider();
AddDateTimeProvider() registers TimeProvider (TimeProvider.System) and IDateTimeProvider
(DateTimeProvider) as singletons. There is nothing to configure.
Usage
public class OrderService(IDateTimeProvider clock)
{
public Order Create(string customerId) => new()
{
CustomerId = customerId,
CreatedAt = clock.OffsetUtcNow, // DateTimeOffset with a zero offset
ExpiresAt = clock.OffsetUtcNow.AddDays(30),
};
public bool IsExpired(Order order) => order.ExpiresAt < clock.OffsetUtcNow;
}
In a test, hand DateTimeProvider any TimeProvider, for example FakeTimeProvider from the
Microsoft.Extensions.TimeProvider.Testing package:
var time = new FakeTimeProvider(new DateTimeOffset(2026, 6, 1, 12, 0, 0, TimeSpan.Zero));
var service = new OrderService(new DateTimeProvider(time));
var order = service.Create("customer-1");
time.Advance(TimeSpan.FromDays(31));
Assert.True(service.IsExpired(order));
Members of IDateTimeProvider |
Value |
|---|---|
UtcNow / Now |
DateTime in UTC (Kind == Utc) / in the local time zone of the clock (Kind == Local) |
OffsetUtcNow / OffsetNow |
DateTimeOffset with a zero offset / with the offset of the clock's local time zone |
UtcToday / Today |
The current date with the time set to midnight, in UTC / in the local time zone |
UtcTimeOfDay / TimeOfDay |
TimeSpan since midnight, in UTC / in the local time zone |
Good to know
- A
TimeProviderorIDateTimeProviderthat is already registered is kept. Register your ownTimeProviderbefore or afterAddDateTimeProvider();IDateTimeProviderreads the one the container resolves. - "Local" means the local time zone of the
TimeProviderin use, not necessarily the zone of the machine. - For values that are stored or sent, use
OffsetUtcNoworOffsetNow: aDateTimeOffsetkeeps the offset from UTC, aDateTimeonly hints at it throughKind. OffsetNow,OffsetUtcNowandUtcTimeOfDayhave default implementations in the interface, so your own implementation or a mock only has to provideNow,UtcNow,TimeOfDay,TodayandUtcToday.UtcTimeOfDatyis the misspelled, obsolete name ofUtcTimeOfDay; it still works.new DateTimeProvider()reads the system clock.DateTimeProvider.TimeProviderexposes the clock in use.
Documentation
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. net11.0 is compatible. |
-
net10.0
-
net11.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 3.0.0-preview.2 | 50 | 11/12/2026 |
| 3.0.0-preview.1 | 52 | 11/12/2026 |
| 2.1.0 | 97 | 11/12/2026 |
| 2.0.1 | 85 | 11/12/2026 |
| 2.0.0 | 102 | 11/12/2026 |
| 1.6.0 | 88 | 11/12/2026 |
| 1.4.19 | 105 | 10/26/2026 |
| 1.4.18 | 100 | 10/23/2026 |
| 1.4.17 | 111 | 10/6/2026 |
| 1.4.16 | 114 | 9/28/2026 |
| 1.4.15 | 124 | 9/21/2026 |
| 1.4.14 | 134 | 8/20/2026 |
| 1.4.13 | 131 | 8/20/2026 |
| 1.4.12 | 140 | 8/20/2026 |
| 1.4.11 | 135 | 8/7/2026 |
| 1.4.10 | 160 | 7/8/2026 |
| 1.4.9 | 145 | 7/8/2026 |
| 1.4.8 | 145 | 7/6/2026 |
| 1.4.7 | 149 | 5/20/2026 |
| 1.4.6 | 141 | 5/20/2026 |