Introduction: Why Working with Dates in C# is Important
In modern software development, working with dates and times is one of the most frequent and critically important tasks. Whether it's event logging, deadline calculations, displaying news, or generating reports — accurate and flexible management of temporal data is required everywhere. C# and the .NET platform provide developers with a powerful and intuitive set of tools for this purpose.
In this article, we will thoroughly examine the main types for working with dates and times in C#: DateTime, TimeSpan, and DateTimeOffset. You will learn how to create, modify, format, and compare dates, as well as how to properly work with time zones. The material will be useful for both beginners and experienced developers looking to systematize their knowledge.
Main Data Types: DateTime, TimeSpan, and DateTimeOffset
1. The DateTime Structure
DateTime is the primary type for representing a point in time (date + time). It stores information from January 1, year 0001, to December 31, year 9999, with an accuracy of 100 nanoseconds (a tick).
Creating a DateTime object:
// Current date and timeDateTime now = DateTime.Now;// Current date and time in UTCDateTime utcNow = DateTime.UtcNow;// Specific dateDateTime specificDate = new DateTime(2025, 3, 15);// Date and time with second precisionDateTime specificDateTime = new DateTime(2025, 3, 15, 14, 30, 0);// Today's date (time = 00:00:00)DateTime today = DateTime.Today;2. The TimeSpan Structure
TimeSpan represents a time interval (a duration). It is not tied to a specific date but stores a difference in time: days, hours, minutes, seconds, and fractions of a second.
Creating and using TimeSpan:
// Creating an interval: 2 days, 5 hours, 30 minutesTimeSpan interval = new TimeSpan(2, 5, 30, 0);// Interval from hoursTimeSpan fromHours = TimeSpan.FromHours(48.5);// Difference between two datesDateTime start = new DateTime(2025, 1, 1);DateTime end = new DateTime(2025, 3, 15);TimeSpan difference = end - start;Console.WriteLine(difference.Days); // 733. The DateTimeOffset Structure
DateTimeOffset is a more modern alternative to DateTime that, in addition to date and time, stores the offset relative to UTC (e.g., +03:00). This helps avoid confusion with time zones when transferring data between systems.
// Current time with offsetDateTimeOffset localOffset = DateTimeOffset.Now;// UTC time with offset +00:00DateTimeOffset utcOffset = DateTimeOffset.UtcNow;// Creating with a specified offsetDateTimeOffset specific = new DateTimeOffset(2025, 3, 15, 12, 0, 0, TimeSpan.FromHours(3));Basic Operations: Addition, Subtraction, and Comparison
Arithmetic with DateTime and TimeSpan
One of the main advantages of C# is convenient date arithmetic. You can easily add or subtract days, months, years, as well as work with hours and minutes.
DateTime today = DateTime.Today;// Add 10 daysDateTime futureDate = today.AddDays(10);// Add 2 monthsDateTime futureMonth = today.AddMonths(2);// Subtract 1 yearDateTime pastYear = today.AddYears(-1);// Add 5 hours and 30 minutesDateTime precise = today.AddHours(5).AddMinutes(30);Working with TimeSpan:
TimeSpan workDay = new TimeSpan(8, 0, 0); // 8 hoursTimeSpan breakTime = new TimeSpan(0, 45, 0); // 45 minutesTimeSpan totalWork = workDay + breakTime; // 8 hours 45 minutesTimeSpan remaining = workDay - breakTime; // 7 hours 15 minutesComparing Dates
Standard comparison operators are used to compare two points in time:
Da