Soenneker.Extensions.DateTime
4.0.1782
Prefix Reserved
dotnet add package Soenneker.Extensions.DateTime --version 4.0.1782
NuGet\Install-Package Soenneker.Extensions.DateTime -Version 4.0.1782
<PackageReference Include="Soenneker.Extensions.DateTime" Version="4.0.1782" />
<PackageVersion Include="Soenneker.Extensions.DateTime" Version="4.0.1782" />
<PackageReference Include="Soenneker.Extensions.DateTime" />
paket add Soenneker.Extensions.DateTime --version 4.0.1782
#r "nuget: Soenneker.Extensions.DateTime, 4.0.1782"
#:package Soenneker.Extensions.DateTime@4.0.1782
#addin nuget:?package=Soenneker.Extensions.DateTime&version=4.0.1782
#tool nuget:?package=Soenneker.Extensions.DateTime&version=4.0.1782
Soenneker.Extensions.DateTime
Date-time helpers for time-zone wall clocks, calendar differences, period boundaries, unit-based arithmetic, offsets, and common string formats.
Installation
dotnet add package Soenneker.Extensions.DateTime
Time-zone conversion
using Soenneker.Extensions.DateTime;
TimeZoneInfo eastern = TimeZoneInfo.FindSystemTimeZoneById("America/New_York");
System.DateTime utc = new(2026, 8, 29, 18, 0, 0, DateTimeKind.Utc);
System.DateTime easternWallClock = utc.ToTz(eastern);
System.DateTime enteredWallClock = new(2026, 8, 29, 14, 0, 0);
System.DateTime convertedUtc = enteredWallClock.ToUtc(eastern);
ToUtc() interprets the input fields as a wall-clock value in the supplied time zone. Its original Kind is ignored, and the result is a UTC instant.
ToTz() has intentionally different semantics from a normal instant-preserving conversion: it converts a UTC instant to the target wall clock and then labels that wall-clock value as DateTimeKind.Utc. The returned fields show target-zone time, but the value must not be persisted or compared as the original UTC instant. Use DateTimeOffset or TimeZoneInfo.ConvertTimeFromUtc() when you need conventional instant semantics.
Differences and age
System.DateTime createdAt = new(2025, 1, 15, 12, 0, 0, DateTimeKind.Utc);
System.DateTime measuredAt = new(2026, 3, 1, 12, 0, 0, DateTimeKind.Utc);
double ageInDays = createdAt.ToAge(UnitOfTime.Day, measuredAt);
double months = DateTimeExtension.MonthsBetween(createdAt, measuredAt);
int completeYears = DateTimeExtension.WholeYearsBetween(createdAt, measuredAt);
ToAge() supports ticks through years. Fixed units use elapsed duration; months, quarters, and years use calendar intervals. Past values are positive and future values are negative.
MonthsBetween(), QuartersBetween(), and YearsBetween() are non-negative regardless of argument order and include fractional progress through the next calendar interval. Their Whole...Between() counterparts discard the incomplete interval.
Period boundaries
System.DateTime value = new(2026, 8, 29, 16, 42, 30, DateTimeKind.Utc);
System.DateTime startOfMonth = value.ToStartOf(UnitOfTime.Month);
System.DateTime endOfMonth = value.ToEndOf(UnitOfTime.Month);
System.DateTime startOfWeek = value.Trim(UnitOfTime.Week);
Trim() and ToStartOf() return the first tick of the selected period. TrimEnd() and ToEndOf() return one tick before the next period. Weeks begin on Monday, quarters begin in January/April/July/October, and decades begin at years divisible by ten. The input Kind is preserved unless the optional dateTimeKind argument overrides it.
Boundary operations support microseconds, milliseconds, seconds, minutes, hours, days, weeks, months, quarters, years, and decades.
Unit-based arithmetic
System.DateTime delayed = value.Add(1.5, UnitOfTime.Hour);
System.DateTime previousQuarter = value.Subtract(1, UnitOfTime.Quarter);
(System.DateTime startAt, System.DateTime endAt) =
value.ToWindow(delay: 5, subtraction: 30, UnitOfTime.Minute);
Add() and Subtract() support ticks, nanoseconds, microseconds, milliseconds, seconds, minutes, hours, days, weeks, months, quarters, years, and decades. DateTime stores 100-nanosecond ticks, so sub-tick portions of nanosecond or microsecond inputs are truncated. Fractional months and years are converted using the length of the calendar month or year reached after adding the whole portion.
ToWindow() first subtracts delay to produce endAt, then subtracts subtraction from that result to produce startAt.
Other helpers
IsBetween()uses inclusive start and end boundaries.ToDateAsInteger()returnsyyyyMMddas an integer.ToUnixTimeSeconds()usesDateTimeOffsetconversion rules, so the inputKindmatters.ToDateTimeOffset()follows the framework constructor: UTC receives offset zero; Local and Unspecified use the machine's local zone.ToUtcKind()andToUnspecifiedKind()only relabelKind; they do not convert clock fields.ToTzOffset()returns the applicable offset for the supplied UTC instant, including daylight-saving rules.ToTzOffsetHours()returns only the whole-hour component; useToTzOffset()for half-hour and quarter-hour zones.ToUtcHoursFromTz()converts a local hour to a UTC hour from0through23using the offset applicable at the supplied instant.
Formatting
The formatting helpers include:
| Method | Output |
|---|---|
ToPreciseFormat() |
yyyy-MM-ddTHH:mm:ss.fffffff |
ToPreciseUtcFormat() |
Same format plus literal Z |
ToIso8601() / ToWebString() |
yyyy-MM-ddTHH:mm:ss.fffZ |
ToMonthFirstDateFormat() |
MM-dd-yyyy |
ToFileName() |
yyyy-MM-dd--HH-mm-ss |
ToShortMonthDayYearString() |
Invariant MMM dd, yyyy |
ToLongMonthDayYearString() |
Invariant MMMM d, yyyy |
The UTC-named formatters append a literal Z; they do not convert the value first. The ToTz...Format() methods perform the library's ToTz() wall-clock conversion and append the time-zone abbreviation supplied by Soenneker.Extensions.TimeZoneInfos.
| 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. |
-
net10.0
- Soenneker.Enums.UnitOfTime (>= 4.0.601)
- Soenneker.Extensions.TimeZoneInfos (>= 4.0.17)
NuGet packages (12)
Showing the top 5 NuGet packages that depend on Soenneker.Extensions.DateTime:
| Package | Downloads |
|---|---|
|
Soenneker.Extensions.DateTime.Day
A collection of helpful DateTime day-based extension methods |
|
|
Soenneker.Extensions.DateTime.Month
A collection of helpful DateTime month-based extension methods |
|
|
Soenneker.Extensions.DateTime.Week
A collection of helpful DateTime week-based extension methods |
|
|
Soenneker.GitHub.Repositories.PullRequests
A utility library for GitHub repository pull request related operations |
|
|
Soenneker.Extensions.DateTime.Quarter
A collection of helpful DateTime quarter (year) based extension methods |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 4.0.1782 | 0 | 8/30/2026 |
| 4.0.1781 | 0 | 8/30/2026 |
| 4.0.1780 | 0 | 8/30/2026 |
| 4.0.1779 | 0 | 8/29/2026 |
| 4.0.1778 | 63 | 8/29/2026 |
| 4.0.1777 | 38 | 8/29/2026 |
| 4.0.1776 | 65 | 8/29/2026 |
| 4.0.1775 | 972 | 8/25/2026 |
| 4.0.1774 | 1,412 | 8/21/2026 |
| 4.0.1773 | 1,230 | 8/18/2026 |
| 4.0.1772 | 368 | 8/18/2026 |
| 4.0.1771 | 2,731 | 8/7/2026 |
| 4.0.1770 | 1,946 | 7/28/2026 |
| 4.0.1769 | 328 | 7/28/2026 |
| 4.0.1768 | 174 | 7/28/2026 |
| 4.0.1767 | 205 | 7/28/2026 |
| 4.0.1766 | 3,218 | 7/17/2026 |
| 4.0.1765 | 334 | 7/17/2026 |
| 4.0.1764 | 662 | 7/16/2026 |
| 4.0.1763 | 578 | 7/16/2026 |
Update dependency Soenneker.Extensions.TimeZoneInfos to 4.0.17 (#2160)