budul.DateTimeExtensions
2.0.0
dotnet add package budul.DateTimeExtensions --version 2.0.0
NuGet\Install-Package budul.DateTimeExtensions -Version 2.0.0
<PackageReference Include="budul.DateTimeExtensions" Version="2.0.0" />
<PackageVersion Include="budul.DateTimeExtensions" Version="2.0.0" />
<PackageReference Include="budul.DateTimeExtensions" />
paket add budul.DateTimeExtensions --version 2.0.0
#r "nuget: budul.DateTimeExtensions, 2.0.0"
#:package budul.DateTimeExtensions@2.0.0
#addin nuget:?package=budul.DateTimeExtensions&version=2.0.0
#tool nuget:?package=budul.DateTimeExtensions&version=2.0.0
DateTimeExtensions
Extension methods for parsing, generating and transforming date and time data, with a focus on operating calendars: date lists and periods from strings, day bitmasks, moving dates into periods and tolerant time parsing.
Installation
dotnet add package budul.DateTimeExtensions
Targets net10.0.
Upgrading to 2.0
- The package targets
net10.0only,netstandard2.0,netstandard2.1,net8.0andnet9.0are no longer supported. Stay on 1.x for those. GetDates(from, to, daysOfWeek)returns dates without time of day. The last day is no longer lost if its time of day is earlier than the one offrom.GetDatesandGetPeriodsthrow anArgumentExceptionfor anullor empty separator.GetDatesfrom a string no longer returns duplicates for overlapping sections.- A single value with time of day in
GetPeriodsends at the end of its own day instead of the following day. ToTimeSpanonly accepts inputs which are completely a time. Minutes and seconds must be below 60, and inputs like2020-01-10or123456789returnnull.ToTimeSpanwith delimiters never interprets the input as fraction of a day. Non-finite, negative and out of range numbers returnnullinstead of throwing.MoveInPeriodkeeps theDateTimeKindof the input.ToTimeStringno longer doubles the sign for standard formats like"c".
Overview
| Area | Methods |
|---|---|
| Date lists and periods | GetDates, GetPeriods |
| Bitmasks | GetDates (from bitmask), GetBits, ToBitmask |
| Period handling | MoveInPeriod, Shift, GetNext, GetPrevious |
| Time parsing | ToTimeSpan |
| Formatting | ToDateString, ToTimeString, ToTotalTimeString |
| Helpers | GetAbsDuration, AddDays, TimeOfDay, ToDateTime, ToUnspecified |
Usage
using DateTimeExtensions;
Dates and periods from strings
Sections are separated by , (configurable), ranges by >.
// 2020-01-10, 2020-01-12, 2020-01-13, 2020-01-14
var dates = "2020-01-10,2020-01-12>2020-01-14".GetDates();
// Custom section separator
var other = "2020-01-10;2020-01-12".GetDates(";");
// (2020-01-10 00:00, 2020-01-10 23:59:59.9999999)
// (2020-01-12 18:00, 2020-01-14 10:00)
var periods = "2020-01-10,2020-01-12 18:00>2020-01-14 10:00".GetPeriods();
foreach (var (from, to) in periods)
{
// ...
}
Notes:
- A single date or a range end without time of day covers the whole day. A single value with time of day covers the rest of its own day. A range end at
00:00is treated as a pure date and covers the whole day too. - Unparsable sections are skipped. A range with a start later than its end throws a
FormatException. >cannot be used as section separator (ArgumentException).- Parsing uses
DateTime.TryParsewith the current culture. ISO 8601 (yyyy-MM-dd) is recommended.
Bitmasks
A bitmask is a string where each character represents one day, starting at a given date.
// Every Monday between 2020-01-06 and 2020-01-19: 2020-01-06, 2020-01-13
var mondays = "1000000".GetDates(
startDate: new DateTime(2020, 1, 6),
endDate: new DateTime(2020, 1, 19));
// Without end date, the mask is applied once
var once = "0001000".GetDates(new DateTime(2020, 1, 6)); // 2020-01-09
// Custom positive character
var custom = "YNY".GetDates(new DateTime(2020, 1, 6), positiveBit: 'Y');
// Dates to bitmask, time of day is ignored: "101"
var mask = new[] { new DateTime(2020, 1, 10), new DateTime(2020, 1, 12) }.ToBitmask();
// Bit indices: { 0, 2 }
var bits = "101".GetBits();
// Indices to bitmask, with explicit length to keep trailing zeros: "0001000"
var fixedLength = new[] { 3 }.ToBitmask(length: 7);
Without explicit length, ToBitmask ends at the last positive bit. Empty input returns string.Empty,
or null with defaultOnEmpty: true.
Moving dates into a period
var period = new[] { new DateTime(2020, 1, 10), new DateTime(2020, 1, 16) };
// Inside the period: unchanged
new DateTime(2020, 1, 12).MoveInPeriod(period); // 2020-01-12
// Non-cyclic: shifted by exactly one period length
new DateTime(2020, 1, 24).MoveInPeriod(period, isCyclic: false); // 2020-01-17
// Cyclic: wrapped into the period
new DateTime(2020, 1, 24).MoveInPeriod(period, isCyclic: true); // 2020-01-10
The time of day of the input is preserved. Overloads exist for DateTime? and IEnumerable<DateTime>.
Weekdays
var from = new DateTime(2020, 1, 1); // Wednesday
from.GetNext(DayOfWeek.Friday); // 2020-01-03
from.GetPrevious(DayOfWeek.Monday); // 2019-12-30
from.GetNext(DayOfWeek.Wednesday); // 2020-01-01, same day is returned
// All Tuesdays and Fridays in a range
from.GetDates(new DateTime(2020, 1, 31), new[] { DayOfWeek.Tuesday, DayOfWeek.Friday });
Parsing times
ToTimeSpan accepts a range of common formats and returns null if the input cannot be parsed.
| Input | Result |
|---|---|
"13:20", "13:20:55" |
13:20:00, 13:20:55 |
"1320", "132055" |
13:20:00, 13:20:55 |
"12:02:30 AM", "1:15 p.m." |
00:02:30, 13:15:00 |
"1.06:00" |
1.06:00:00 (day prefix) |
"06:00[+1]" |
1.06:00:00 (day offset suffix) |
"0.25399" |
06:05:44 (OLE Automation fraction of a day) |
"10.05.18" with ToTimeSpan(".") |
10:05:18 (custom delimiters) |
Formatting
new DateTime(2020, 1, 10, 8, 0, 0).ToDateString(); // "2020-01-10"
TimeSpan.FromMinutes(-30).ToTimeString(); // "-00:30:00"
// ToTimeString uses the hour component and drops days
new TimeSpan(1, 1, 10, 0).ToTimeString(); // "01:10:00"
// ToTotalTimeString uses total hours, e.g. for service days beyond midnight
new TimeSpan(1, 1, 10, 0).ToTotalTimeString(); // "25:10:00"
License
| 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
- 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 |
|---|---|---|
| 2.0.0 | 112 | 11/26/2026 |
| 1.20.0 | 156 | 10/2/2026 |
| 1.19.0 | 2,879 | 11/7/2025 |
| 1.18.1 | 2,851 | 8/13/2025 |
| 1.18.0 | 2,845 | 8/13/2025 |
| 1.17.1 | 2,885 | 4/28/2024 |
| 1.17.0 | 2,876 | 4/25/2024 |
| 1.16.1 | 2,818 | 4/25/2024 |
| 1.16.0 | 2,821 | 4/25/2024 |
| 1.15.0 | 3,011 | 7/26/2023 |
| 1.14.1 | 3,071 | 3/21/2023 |
| 1.14.0 | 3,057 | 2/14/2023 |
| 1.13.0 | 3,054 | 2/12/2023 |
| 1.12.2 | 3,069 | 2/8/2023 |
| 1.12.1 | 3,198 | 11/30/2021 |
| 1.12.0 | 3,152 | 11/30/2021 |
| 1.11.0 | 3,198 | 3/9/2021 |
| 1.10.0 | 3,216 | 2/10/2021 |
| 1.9.0 | 3,361 | 11/3/2020 |
| 1.8.0 | 3,358 | 11/1/2020 |