Jacobi.DateTimeOperators 1.1.0

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

Jacobi DateTime Operators

This package implements operator extensions for DateTime, DateTimeOffset, DateOnly, TimeOnly and TimeSpan.

Note that all of this code is wrapper code and all operations for date-time are implemented using existing methods on the DateTime (and related) types. This will bring down the chance of bugs or inconsistancies.

Key Advantages

  1. Readability: Mathematical operations look more natural
  2. Maintainability: Less boilerplate code
  3. Safety: Type-safe generic constraints ensure valid operations
  4. Expressiveness: Natural syntax for manipulating date-time

Usage

Here are some usage examples:

var dt = new DateTime(2025, 11, 21);
var dt1 = dt + 3.Days;
var dt = new DateTime(2025, 11, 21);
var dt1 = dt - 3.M; // Months
var dt = new DateTime(2025, 11, 21, 14, 42, 12);
var dt1 = dt + 7.5.Minutes; // add 7 and a half minutes
// dt1.Minute => 49 (42+7)
// dt1.Second => 42 (12+30)

Type System

The library provides (value) types for every part that plays a role in manipulating date-time.

Type Factory Value Description
Ticks t, Ticks long Represents time ticks (100 nanosecond units)
Microseconds us, Microseconds long, int Represents microseconds
MicrosecondsF us, Microseconds double Represents fractional microseconds
Milliseconds ms, Milliseconds long, int Represents milliseconds
MillisecondsF ms, Milliseconds double Represents fractional milliseconds
Seconds s, Seconds long, int Represents seconds
SecondsF s, Seconds double Represents fractional seconds
Minutes m, Minutes long, int Represents minutes
MinutesF m, Minutes double Represents fractional minutes
Hours h, Hours int, Represents hours
HoursF h, Hours double Represents fractional hours
Days d, Days int Represents days
DaysF d, Days double Represents fractional days
Months M, Months int Represents months
Years y, Years int Represents years

Examples of how to use the type system:

var years = new Years(2025)   // all value types have a value constructor
var days = 3.d;               // d extension on int returns a Days instance
var days2 = 3.Days;           // use the full name of the extension for readability/preference
var secondsFraction = 2.4.s   // s extension on double returns a SecondsF instance
var ticks = 843710982.t       // t extension on long returns a Ticks instance

Operator Extensions Reference

Types DateTime DateTimeOffset DateOnly TimeOnly* TimeSpan*
Ticks +, - +, -
Microseconds +, - +, - +, - +, -
MicrosecondsF +, - +, - +, - +, -
Milliseconds +, - +, - +, - +, -
MillisecondsF +, - +, - +, - +, -
Seconds +, - +, - +, - +, -
SecondsF +, - +, - +, - +, -
Minutes +, - +, - +, - +, -
MinutesF +, - +, - +, - +, -
Hours +, - +, - +, - +, -
HoursF +, - +, - +, - +, -
Days +, - +, - +, - +, -
DaysF +, - +, - +, -
Months +, - +, - +, -
Years +, - +, - +, -

*) Some operators may introduce an additional value transformation to/from TimeSpan.

Note that TimeSpan already has several overloaded operators for primitive types and arithmetic functions.

Extension Methods

From (static)

All date-time types support From methods for construction with typed parameters:

var years = new Years(2025);
var months = new Months(11);
var days = new Days(21);
var dt = DateTime.From(years, months, days);

The From extension method for DateTimeOffset requires an extra offset parameter (TimeSpan).

Multiple overloads are available per type with different combinations of parameter types.

To

Because the DateTime and related type already have a Deconstruct implementation with 2 and 3 parameters (and for TimeOnly even more), we could not use extensions with the same number of parameters. Instead we've renamed these methods to To.

All date-time types support T methods for deconstructing with typed parameters:

var dt = new DateTime(2025, 11, 21);
dt.To(out Years years, out Months months, out Days days);
// years => 2025
// months => 11
// days => 21

The To extension method for DateTimeOffset returns the offset as a return value.

Multiple overloads are available per type with different combinations of parameter types.

Deconstruct

Deconstruct methods with 2 and 3 parameters are not available for our type system. Us the To method instead.

var dt = new DateTime(2025, 11, 21, 14, 34, 45);
(Years years, Months months, Days days, Hours hours, Minutes minutes, Seconds seconds) = dt;
// years => 2025
// months => 11
// days => 21
// hours => 14
// minutes => 34
// seconds => 45

You may also enjoy the Jacobi.ArrayOperators package that lets you do 'array arithmetic' using operator extensions.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.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
1.1.0 690 12/2/2025
1.0.0 209 11/22/2025