Soenneker.Extensions.DateTimeOffsets 4.0.139

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package Soenneker.Extensions.DateTimeOffsets --version 4.0.139
                    
NuGet\Install-Package Soenneker.Extensions.DateTimeOffsets -Version 4.0.139
                    
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="Soenneker.Extensions.DateTimeOffsets" Version="4.0.139" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Soenneker.Extensions.DateTimeOffsets" Version="4.0.139" />
                    
Directory.Packages.props
<PackageReference Include="Soenneker.Extensions.DateTimeOffsets" />
                    
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 Soenneker.Extensions.DateTimeOffsets --version 4.0.139
                    
#r "nuget: Soenneker.Extensions.DateTimeOffsets, 4.0.139"
                    
#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 Soenneker.Extensions.DateTimeOffsets@4.0.139
                    
#: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=Soenneker.Extensions.DateTimeOffsets&version=4.0.139
                    
Install as a Cake Addin
#tool nuget:?package=Soenneker.Extensions.DateTimeOffsets&version=4.0.139
                    
Install as a Cake Tool

alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image

alternate text is missing from this package README image Soenneker.Extensions.DateTimeOffsets

DateTimeOffset helpers for time-zone conversion, elapsed and calendar differences, business days, range checks, period boundaries, unit-based arithmetic, offsets, and invariant formatting.

Installation

dotnet add package Soenneker.Extensions.DateTimeOffsets

Convert an instant

using Soenneker.Extensions.DateTimeOffsets;

DateTimeOffset value = new(2026, 8, 29, 18, 0, 0, TimeSpan.Zero);
TimeZoneInfo eastern = TimeZoneInfo.FindSystemTimeZoneById("America/New_York");

DateTimeOffset easternValue = value.ToTz(eastern);
DateTimeOffset utcValue = easternValue.ToUtc();
DateTime utcDateTime = easternValue.ToUtcDateTime();

ToTz() and ToUtc() preserve the instant while changing its displayed offset. ToUtcDateTime() returns the same instant as a DateTime with Kind set to Utc.

Elapsed and calendar differences

DateTimeOffset from = new(2025, 1, 15, 12, 0, 0, TimeSpan.Zero);
DateTimeOffset to = new(2026, 3, 1, 12, 0, 0, TimeSpan.Zero);

double elapsedDays = from.ToAge(UnitOfTime.Day, to);
double months = DateTimeOffsetExtension.MonthsBetween(from, to);
int completeYears = DateTimeOffsetExtension.WholeYearsBetween(from, to);

ToAge() supports ticks, microseconds, milliseconds, seconds, minutes, hours, days, weeks, months, quarters, and years. Fixed units use elapsed duration. Calendar units use whole intervals plus fractional progress through the next actual month, quarter, or year.

MonthsBetween(), QuartersBetween(), and YearsBetween() are signed: reversing the endpoints reverses the sign. Their Whole...Between() counterparts return a non-negative complete-interval count regardless of argument order.

Business days

CultureInfo culture = CultureInfo.GetCultureInfo("en-US");

bool isBusinessDay = value.IsBusinessDay(eastern, culture);
DateTimeOffset threeBusinessDaysLater = value.AddBusinessDays(3, eastern, culture);

Business-day methods skip weekends only; they do not know public holidays. Weekend selection comes from Soenneker.Extensions.CultureInfos, whose fixed rules map ar-*, he-IL, fa-IR, and ur-PK to Friday–Saturday and every other culture to Saturday–Sunday.

When a time zone is supplied, it is used to decide each candidate instant's local weekday. AddBusinessDays() still advances the original DateTimeOffset by 24-hour increments and preserves its stored offset; it does not preserve a target-zone wall-clock time across DST or replace the result's offset with the zone's offset.

Boundaries and ranges

DateTimeOffset startOfMonth = value.ToStartOf(UnitOfTime.Month);
DateTimeOffset endOfMonth = value.ToEndOf(UnitOfTime.Month);
bool inside = value.IsBetween(startOfMonth, endOfMonth);

Trim() / ToStartOf() return the first tick of a period. TrimEnd() / ToEndOf() return one tick before the next period. Supported boundaries are microsecond through decade; weeks begin Monday and quarters begin in January, April, July, and October.

Boundary methods operate in the value's existing offset and preserve that offset. They do not consult a TimeZoneInfo, so a fixed offset cannot automatically follow DST changes across a long period.

IsBetween() accepts endpoints in either order. Bounds are inclusive by default; pass inclusive: false for a strict comparison.

Unit-based arithmetic

DateTimeOffset delayed = value.Add(1.5, UnitOfTime.Hour);
DateTimeOffset previousQuarter = value.Subtract(1, UnitOfTime.Quarter);
(DateTimeOffset startAt, DateTimeOffset endAt) =
    value.ToWindow(delay: 5, subtraction: 30, UnitOfTime.Minute);

Add() and Subtract() support ticks through decades. DateTimeOffset stores 100-nanosecond ticks, so sub-tick nanosecond and microsecond portions are truncated. Fractional months, quarters, years, and decades are converted using the length of the calendar month or year reached after the whole portion is applied.

ToWindow() subtracts delay to produce endAt, then subtracts subtraction from that endpoint to produce startAt.

Time-zone offsets and local hours

TimeSpan offset = value.ToTzOffset(eastern);
double offsetHours = value.ToTzOffsetHours(eastern);
int utcHour = value.ToUtcHoursFromTz(tzHour: 9, eastern);

Offset lookup uses the supplied instant, including the zone's daylight-saving rule at that instant. ToTzOffsetHours() returns double, preserving half-hour and quarter-hour offsets.

ToUtcHoursFromTz() chooses the local calendar date corresponding to value, interprets tzHour on that date, and returns only the UTC hour from 0 through 23. Invalid local times advance to the first valid minute; ambiguous times choose the earlier UTC instant. Because only an hour is returned, date rollover and minutes are not represented.

Formatting

Formatting is invariant-culture. Methods without Tz format the value's existing clock fields; ToTz...() methods first convert the instant to the supplied zone.

Common formats include:

Methods Output shape
ToHourFormat(), ToHourMinuteFormat(), ToHourMinuteSecondFormat() hh tt, h:mm tt, h:mm:ss tt
To24HourFormat(), To24HourMinuteFormat(), To24HourMinuteSecondFormat() HH, HH:mm, HH:mm:ss
ToDateFormat(), ToDateDashFormat(), ToYearMonthDayFormat() MM/dd/yyyy, MM-dd-yyyy, yyyy-MM-dd
ToSortableMinuteFormat(), ToSortableSecondFormat() yyyy-MM-dd HH:mm[:ss]
ToPreciseFormat() yyyy-MM-ddTHH:mm:ss.fffffff using the stored offset's clock fields
ToPreciseUtcFormat() UTC with seven fractional digits and literal Z
ToIso8601() / ToWebString() UTC with milliseconds and literal Z
ToIso8601SecondFormat(), ToIso8601MillisFormat() Stored clock fields without an offset suffix
ToFileName(), ToFileNameMillis() File-safe timestamps

Time-zone display methods that append an abbreviation use Soenneker.Extensions.TimeZoneInfos. ToDateTimeFormatAsTz() is the exception to conversion: it formats the existing clock fields and uses the supplied zone only for its abbreviation.

ToDateAsInteger() and ToDateOnly() use the value's stored local calendar fields; they do not normalize to UTC first.

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

NuGet packages (9)

Showing the top 5 NuGet packages that depend on Soenneker.Extensions.DateTimeOffsets:

Package Downloads
Soenneker.Extensions.TimeSpan

A collection of helpful TimeSpan extension methods

Soenneker.AdaptiveCard.Util

A utility library for Adaptive Card construction

Soenneker.Extensions.Logger

A collection of helpful ILogger extension methods

Soenneker.Extensions.DateTimeOffsets.Days

A collection of helpful DateTimeOffset day extension methods

Soenneker.Extensions.DateTimeOffsets.Months

A collection of helpful DateTimeOffset month extension methods

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
4.0.143 318 9/1/2026
4.0.142 338 8/31/2026
4.0.141 252 8/31/2026
4.0.140 267 8/31/2026
4.0.139 80 8/31/2026
4.0.138 499 8/30/2026
4.0.137 84 8/30/2026
4.0.136 129 8/30/2026
4.0.135 210 8/30/2026
4.0.134 120 8/30/2026
4.0.133 94 8/29/2026
4.0.132 573 8/29/2026
4.0.131 1,387 8/26/2026
4.0.130 397 8/25/2026
4.0.129 293 8/25/2026
4.0.128 667 8/22/2026
4.0.127 752 8/21/2026
4.0.126 1,137 8/18/2026
4.0.125 747 8/18/2026
4.0.124 508 8/18/2026
Loading failed

Update dependency Soenneker.Enums.UnitOfTime to 4.0.602 (#292)