TailoredApps.Shared.DateTime 3.0.0-preview.2

This is a prerelease version of TailoredApps.Shared.DateTime.
dotnet add package TailoredApps.Shared.DateTime --version 3.0.0-preview.2
                    
NuGet\Install-Package TailoredApps.Shared.DateTime -Version 3.0.0-preview.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TailoredApps.Shared.DateTime" Version="3.0.0-preview.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TailoredApps.Shared.DateTime" Version="3.0.0-preview.2" />
                    
Directory.Packages.props
<PackageReference Include="TailoredApps.Shared.DateTime" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TailoredApps.Shared.DateTime --version 3.0.0-preview.2
                    
#r "nuget: TailoredApps.Shared.DateTime, 3.0.0-preview.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package TailoredApps.Shared.DateTime@3.0.0-preview.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=TailoredApps.Shared.DateTime&version=3.0.0-preview.2&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=TailoredApps.Shared.DateTime&version=3.0.0-preview.2&prerelease
                    
Install as a Cake Tool

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 TimeProvider or IDateTimeProvider that is already registered is kept. Register your own TimeProvider before or after AddDateTimeProvider(); IDateTimeProvider reads the one the container resolves.
  • "Local" means the local time zone of the TimeProvider in use, not necessarily the zone of the machine.
  • For values that are stored or sent, use OffsetUtcNow or OffsetNow: a DateTimeOffset keeps the offset from UTC, a DateTime only hints at it through Kind.
  • OffsetNow, OffsetUtcNow and UtcTimeOfDay have default implementations in the interface, so your own implementation or a mock only has to provide Now, UtcNow, TimeOfDay, Today and UtcToday.
  • UtcTimeOfDaty, the misspelled name of UtcTimeOfDay, was removed in 3.0.
  • new DateTimeProvider() reads the system clock. DateTimeProvider.TimeProvider exposes the clock in use.

Documentation

https://shared.tailoredapps.pl/en/Libraries/DateTime/

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
Loading failed