CSharpEssentials.Time 8.0.0

dotnet add package CSharpEssentials.Time --version 8.0.0
                    
NuGet\Install-Package CSharpEssentials.Time -Version 8.0.0
                    
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="CSharpEssentials.Time" Version="8.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CSharpEssentials.Time" Version="8.0.0" />
                    
Directory.Packages.props
<PackageReference Include="CSharpEssentials.Time" />
                    
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 CSharpEssentials.Time --version 8.0.0
                    
#r "nuget: CSharpEssentials.Time, 8.0.0"
                    
#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 CSharpEssentials.Time@8.0.0
                    
#: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=CSharpEssentials.Time&version=8.0.0
                    
Install as a Cake Addin
#tool nuget:?package=CSharpEssentials.Time&version=8.0.0
                    
Install as a Cake Tool

CSharpEssentials.Time

A testable time abstraction built on .NET TimeProvider. Code that reads the time through IDateTimeProvider does not depend on the system clock, so unit tests can control the time.

Benchmarks of the clock reads (UtcNow, UtcNowDateTime, UtcNowDate, UtcNowTime) against TimeProvider.System and DateTimeOffset.UtcNow, and of the DateOnly, TimeOnly, weekday and age helpers against the BCL and hand-written code, are in BENCHMARKS.md.

Installation

dotnet add package CSharpEssentials.Time

Targets net11.0, net10.0, net9.0 and netstandard2.1 (which adds Microsoft.Bcl.TimeProvider). The DateOnly and TimeOnly members (UtcNowDate, UtcNowTime, ToDateOnly, ToTimeOnly, the DateOnly weekday overloads and GetAge) are not in the netstandard2.1 build.

Usage

IDateTimeProvider and DateTimeProvider

using CSharpEssentials.Time;

public class OrderService(IDateTimeProvider time)
{
    public Order CreateOrder() => new()
    {
        CreatedAt = time.UtcNow,
        CreatedDate = time.UtcNowDate
    };
}

builder.Services.AddSingleton<TimeProvider>(TimeProvider.System);
builder.Services.AddSingleton<IDateTimeProvider, DateTimeProvider>();

IDateTimeProvider exposes UtcNow, UtcNowDateTime (always DateTimeKind.Utc), UtcNowDate, UtcNowTime, TimeZone and TimeZoneUtc. DateTimeProvider wraps a TimeProvider, so TimeProvider.System or a test provider such as FakeTimeProvider both work. UtcNow reads TimeProvider.GetUtcNow(), UtcNowDate and UtcNowTime are the UTC date and time of day, TimeZone is TimeZoneInfo.Local and TimeZoneUtc is TimeZoneInfo.Utc.

FakeDateTimeProvider

The package ships FakeDateTimeProvider, a test clock that starts at a fixed time and moves only with Advance and SetTime. Its TimeZone is UTC, and it is safe to use from several threads. The sample asserts with FluentAssertions, which is not a dependency of this package.

var fixedTime = new DateTimeOffset(2025, 1, 15, 10, 0, 0, TimeSpan.Zero);
var fake = new FakeDateTimeProvider(fixedTime);

Order order = new OrderService(fake).CreateOrder();
order.CreatedAt.Should().Be(fixedTime);

fake.Advance(TimeSpan.FromHours(2));
fake.UtcNow.Should().Be(fixedTime.AddHours(2));

fake.SetTime(new DateTimeOffset(2026, 1, 1, 0, 0, 0, TimeSpan.Zero));
fake.UtcNowDate.Should().Be(new DateOnly(2026, 1, 1));
fake.UtcNowTime.Should().Be(new TimeOnly(0, 0, 0));

Extensions

DateTime now = DateTime.UtcNow;

DateOnly date = now.ToDateOnly();
TimeOnly time = now.ToTimeOnly();

DateOnly today = new(2024, 6, 15);                     // Saturday
today.NextDayOfWeek(DayOfWeek.Monday);                 // 2024-06-17
today.PreviousDayOfWeek(DayOfWeek.Saturday);           // 2024-06-08
today.NextDayOfWeek(DayOfWeek.Saturday, includeCurrent: true); // 2024-06-15

new DateOnly(2000, 6, 16).GetAge(today);               // 23
birthDate.GetAge(dateTimeProvider);                    // today in the provider's time zone

NextDayOfWeek and PreviousDayOfWeek exist for DateTime and DateOnly and skip the current day unless includeCurrent: true. GetAge counts whole years and throws ArgumentOutOfRangeException when the birth date is after the reference date.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.1

  • net10.0

    • No dependencies.
  • net11.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on CSharpEssentials.Time:

Package Downloads
CSharpEssentials

A comprehensive C# library enhancing functional programming capabilities with type-safe monads (Maybe, Result), discriminated unions (Any), and robust error handling. Features include: domain-driven design support, enhanced Entity Framework integration, testable time management, JSON utilities, and LINQ extensions. Built for modern C# development with focus on maintainability, testability, and functional programming principles.

CSharpEssentials.Entity

Domain-driven design (DDD) entity base classes and patterns for CSharpEssentials. Provides EntityBase<TId> with soft delete functionality, audit trails, and domain event integration. Essential for building robust domain models with type safety and functional programming principles.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
8.0.0 233 11/9/2026
7.0.0 246 10/20/2026
6.5.2 192 10/10/2026
6.5.1 181 10/10/2026
6.5.0 182 10/9/2026
6.4.0 174 10/9/2026
6.3.0 185 10/8/2026
6.2.0 182 10/8/2026
6.1.0 183 10/7/2026
6.0.0 186 10/7/2026
5.2.1 188 10/7/2026
5.2.0 186 10/7/2026
5.1.0 187 10/7/2026
5.0.0 182 10/6/2026
4.1.0 189 10/6/2026
4.0.0 178 10/5/2026
3.2.3 391 5/31/2026
3.2.2 221 5/31/2026
3.2.1 222 5/31/2026
3.2.0 216 5/30/2026
Loading failed