Working with Dates in C# (.NET): A Complete Overview of Libraries and Built-in Tools
Date and time processing is one of the most common tasks in development. The C# (.NET) ecosystem has both powerful built-in functionality (System.DateTime) and third-party libraries that simplify complex scenarios: working with time zones, formatting, intervals, and parsing. In this article, we will cover the main tools, including modern features of .NET 6+ and popular NuGet packages.
1. Built-in Tools: DateTime, DateOnly, TimeOnly
The base class System.DateTime has been available since the first versions of .NET. It supports:
- Creating a date:
new DateTime(2025, 3, 15) - Arithmetic:
date.AddDays(10),date.AddMonths(-1) - Comparison:
date1 > date2 - Formatting:
date.ToString("yyyy-MM-dd")
Starting with .NET 6, the DateOnly and TimeOnly types appeared — they are not tied to a time zone and store only the date or only the time. This is convenient for birthdays, work schedules, and timetables.
using System;
DateOnly birthday = new DateOnly(1990, 5, 20);TimeOnly alarm = new TimeOnly(7, 30);
Console.WriteLine($"Date: {birthday}, Time: {alarm}");// Output: Date: 20.05.1990, Time: 07:302. TimeSpan — Time Intervals
The TimeSpan is used for working with time intervals. It allows you to calculate the difference between dates, operation durations, and timeouts.
DateTime start = DateTime.Now;// ... code execution ...DateTime end = DateTime.Now;
TimeSpan elapsed = end - start;Console.WriteLine($"Elapsed: {elapsed.TotalSeconds} sec.");3. Noda Time — Advanced Date and Time Handling
Noda Time (NuGet: NodaTime) is a library by Jon Skeet (author of a popular C# book). It solves DateTime problems:
- Clear separation between "a moment in time" (
Instant), "a date in a calendar" (LocalDate), and "time with a zone" (ZonedDateTime). - Support for calendars (Islamic, Hebrew, Japanese).
- Immutability of all types.
- Correct handling of time zones via the IANA (tzdata) database.
using NodaTime;
Instant now = SystemClock.Instance.GetCurrentInstant();DateTimeZone zone = DateTimeZoneProviders.Tzdb["Europe/Moscow"];ZonedDateTime moscowTime = now.InZone(zone);
Console.WriteLine($"Current time in Moscow: {moscowTime:yyyy-MM-dd HH:mm}");
// Working with LocalDateLocalDate today = LocalDate.FromDateTime(DateTime.Today);LocalDate nextWeek = today.PlusDays(7);Noda Time is especially useful in projects where time zone accuracy is critical (logs, bookings, international systems).
4. Humanizer — Human-Readable Dates
Humanizer (NuGet: Humanizer.Core) is a library for converting numbers, dates, and times into natural language. It can:
- Show relative time: "3 minutes ago", "in 2 days".
- Inflect month and day names.
- Format time intervals: "1 hour 30 minutes".
using Humanizer;
DateTime pastDate = DateTime.UtcNow.AddHours(-5);Console.WriteLine(pastDate.Humanize()); // "5 hours ago"
TimeSpan span = new TimeSpan(2, 45, 0);Console.WriteLine(span.Humanize()); // "2 hours 45 minutes"Humanizer supports localization (Russian language — Humanizer.Core.ru).
5. FluentDateTime — Fluent Interface for DateTime
FluentDateTime (NuGet: FluentDateTime) — adds extension methods for more