Extensions for DateOnly, TimeOnly and DateTime in .NET 9. Includes business day calculations, date ranges and useful date/time utilities.
$ dotnet add package DateTimePlus
DateTimePlus is a lightweight extensions library for working with dates and times in .NET 9. It simplifies common operations that normally require repetitive or complex code, such as working with DateOnly, TimeOnly, DateTimeOffset, and date ranges.
DateOnly.NextWeekday() → Gets the next business day.AddBusinessDays(int days) → Adds business days skipping weekends.DaysUntil(DateOnly date) → Calculates the difference in days.MonthsUntil(DateOnly date) → Calculates the difference in months.TimeOnly with DateOnly to obtain a DateTime.DateTimeOffset and time zones.StartOfDay() → Gets the start of the day (00:00:00) preserving the offset.EndOfDay() → Gets the end of the day (23:59:59.999) preserving the offset.IsToday(DateTimeOffset? reference) → Checks if the date is today.IsPast(DateTimeOffset? reference) → Checks if the date is in the past.IsFuture(DateTimeOffset? reference) → Checks if the date is in the future.StartOfDayInTimeZone(TimeZoneInfo timeZone) → Gets the start of the day in a specific time zone.ConvertToTimeZone(DateTimeOffset dateTime, TimeZoneInfo destinationTimeZone) → Converts a date to another time zone.ConvertToTimeZone(DateTimeOffset dateTime, string destinationTimeZoneId) → Converts using a time zone ID string.Days() → Returns all days in the range.BusinessDays() → Returns only business days.Contains(DateOnly date) → Checks if a date is within the range.dotnet add package DateTimePlus --version 1.0.0
var today = DateOnly.FromDateTime(DateTime.Now);
var nextBusinessDay = today.NextWeekday();
var inTwoBusinessDays = today.AddBusinessDays(2);
var date = new DateOnly(2025, 1, 21);
var time = new TimeOnly(14, 30);
var dateTime = time.ToDateTime(date);
var now = DateTimeOffset.Now;
// Get start and end of day
var startOfDay = now.StartOfDay(); // Today at 00:00:00
var endOfDay = now.EndOfDay(); // Today at 23:59:59.999
// Check temporal relationships
if (now.IsToday()) { /* ... */ }
if (someDate.IsPast()) { /* ... */ }
if (futureDate.IsFuture()) { /* ... */ }
// Work with time zones
var madridTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
var startInMadrid = now.StartOfDayInTimeZone(madridTimeZone);
var utcNow = DateTimeOffset.UtcNow;
// Convert to specific time zone
var madridTime = TimeZoneHelper.ConvertToTimeZone(utcNow, "Central European Standard Time");
var newYorkTime = TimeZoneHelper.ConvertToTimeZone(utcNow, "Eastern Standard Time");
// Using TimeZoneInfo directly
var timeZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var pacificTime = TimeZoneHelper.ConvertToTimeZone(utcNow, timeZone);
var start = new DateOnly(2025, 1, 1);
var end = new DateOnly(2025, 1, 31);
var range = new DateRange(start, end);
// Get all days
foreach (var day in range.Days())
{
Console.WriteLine(day);
}
// Get only business days
foreach (var businessDay in range.BusinessDays())
{
Console.WriteLine(businessDay);
}
// Check if date is in range
bool isInRange = range.Contains(new DateOnly(2025, 1, 15));
Contributions are welcome! Feel free to open issues or pull requests on GitHub.
This project is licensed under the MIT License.