Working with Dates and Time in C#: From A to Z

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why Working with Dates and Time in C# is a Foundation for Any Developer

Every C# developer sooner or later faces the need to handle dates and time. Whether it's event logging, calculating deadlines in a business application, or simply displaying the current date on the screen — you cannot do without confident knowledge of DateTime and TimeSpan. In this article, we will cover all key aspects: from creating objects to working with time zones and performance. The material is suitable for both beginners and experienced programmers looking to systematize their knowledge.



Main Types for Working with Date and Time in .NET

In .NET, there are several types, each solving its own task. Let's look at the main ones.



DateTime — The Foundation

DateTime is a structure representing a moment in time (date and time). It supports a range from 01.01.0001 to 31.12.9999. You can create an object in several ways:

// 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 withTime = new DateTime(2025, 3, 15, 14, 30, 0);// Today's date (time 00:00:00)DateTime today = DateTime.Today;


TimeSpan — Time Intervals

TimeSpan represents a time interval (the difference between two dates). It is convenient for calculating operation durations:

// Creating an intervalTimeSpan interval = new TimeSpan(2, 5, 30); // 2 hours, 5 minutes, 30 seconds// Difference between datesDateTime start = DateTime.Now;System.Threading.Thread.Sleep(1000); // simulating workDateTime end = DateTime.Now;TimeSpan duration = end - start;Console.WriteLine($"Elapsed {duration.TotalSeconds} seconds");


DateTimeOffset — Date with Time Zone Offset

For modern applications working with users from different regions, DateTimeOffset is critical. It stores date and time along with the offset from UTC:

// Current time with local offsetDateTimeOffset localOffset = DateTimeOffset.Now;// Time with a specific offsetDateTimeOffset customOffset = new DateTimeOffset(2025, 3, 15, 12, 0, 0, TimeSpan.FromHours(3));// Conversion to UTCDateTime utc = localOffset.UtcDateTime;


Formatting and Parsing Dates

Correctly displaying dates to the user and reading data from strings are essential skills.



Formatting: ToString() and Custom Patterns

The ToString() method accepts a format string. The most popular standard formats:

  • "d" — short date (03/15/2025)
  • "D" — long date (Saturday, March 15, 2025)
  • "t" — short time (14:30)
  • "T" — long time (14:30:00)
  • "g" — general date/time format (03/15/2025 14:30)

Examples of custom formatting:

DateTime dt = new DateTime(2025, 3, 15, 14, 30, 45);Console.WriteLine(dt.ToString("yyyy-MM-dd")); // 2025-03-15Console.WriteLine(dt.ToString("dd.MM.yyyy HH:mm")); // 15.03.2025 14:30Console.WriteLine(dt.ToString("dddd, MMMM yyyy")); // Saturday, March 2025 (culture-dependent)


Parsing: Converting a String to a Date

For reliable parsing, use DateTime.TryParse or DateTime.ParseExact:

string dateString = "15.03.2025";if (DateTime.TryParse(dateString, out DateTime result)){    Console.WriteLine($"Success: {result}");}else{    Console.WriteLine("Parsing error");}

// Exact parsing with format specificationstring exactString = "2025-03-15 14:30:00";DateTime parsed = DateTime.ParseExact(exactString, "yyyy-MM-dd HH:mm

Blogs

Book Recommendations