DateTime & TimeSpan Operations in C#
Learn how to work with DateTime and TimeSpan in C# to perform date and time operations such as comparison, addition, subtraction, and formatting.
In C#, the DateTime and TimeSpan structures are used for working with dates and times.
DateTime represents a specific moment, while TimeSpan represents the difference between two dates, i.e., a time interval.
With these structures, operations such as comparing dates, adding/subtracting time, and formatting can easily be performed.
What is DateTime?
The DateTime structure represents a specific date and time.
DateTime now = DateTime.Now;
Console.WriteLine(now); // Current date and time
Console.WriteLine(DateTime.UtcNow); // Universal time
Basic Properties of DateTime
You can extract individual date and time components:
DateTime date = new DateTime(2025, 9, 9, 14, 30, 0);
Console.WriteLine(date.Year); // 2025
Console.WriteLine(date.Month); // 9
Console.WriteLine(date.Day); // 9
Console.WriteLine(date.Hour); // 14
Console.WriteLine(date.Minute); // 30
Console.WriteLine(date.ToShortDateString()); // 9/9/2025
Console.WriteLine(date.ToLongDateString()); // Tuesday, September 9, 2025
Working with DateTime
Use Add methods to add or subtract days, months, or years.
DateTime today = DateTime.Now;
Console.WriteLine(today.AddDays(5)); // 5 days later
Console.WriteLine(today.AddMonths(-1)); // 1 month earlier
DateTime future = today.AddYears(1);
Console.WriteLine(future); // 1 year later
// Difference between two dates
DateTime birth = new DateTime(2000, 1, 1);
TimeSpan diff = today - birth;
Console.WriteLine($"Total days: {diff.Days}");
What is TimeSpan?
TimeSpan represents a time interval.
TimeSpan duration = new TimeSpan(2, 30, 0); // 2 hours 30 minutes
Console.WriteLine(duration.TotalMinutes); // 150 minutes
Using DateTime with TimeSpan
You can add a TimeSpan to a DateTime to create a new date.
DateTime now = DateTime.Now;
TimeSpan threeHours = new TimeSpan(3, 0, 0);
DateTime newDate = now + threeHours;
Console.WriteLine(newDate);
Comparing Dates
Dates can be compared using >, <, ==, or the Compare method.
DateTime d1 = new DateTime(2025, 1, 1);
DateTime d2 = new DateTime(2025, 12, 31);
Console.WriteLine(d1 < d2); // true
Console.WriteLine(DateTime.Compare(d1, d2)); // -1 (d1 is earlier)
Checking whether a date is in a specific year or month:
DateTime date = new DateTime(2025, 9, 9);
bool sameYear = (date.Year == 2025); // true
bool sameMonth = (date.Month == 9); // true
Date Formatting
With the ToString method, you can apply custom formats.
DateTime now = DateTime.Now;
Console.WriteLine(now.ToString("dd.MM.yyyy")); // 09.09.2025
Console.WriteLine(now.ToString("dd.MM.yy")); // 09.09.25
Console.WriteLine(now.ToString("yyyy-MM-dd HH:mm")); // 2025-09-09 14:45
Sample Application: Project Duration and Daily Rate Calculation
Console.Write("Enter project start date (yyyy-MM-dd): ");
DateTime start = DateTime.Parse(Console.ReadLine());
Console.Write("Enter project end date (yyyy-MM-dd): ");
DateTime end = DateTime.Parse(Console.ReadLine());
if (end <= start)
{
Console.WriteLine("Error: End date must be after the start date.");
}
else
{
Console.Write("Enter total project price: ");
decimal price = decimal.Parse(Console.ReadLine());
TimeSpan duration = end - start;
int days = duration.Days;
decimal dailyRate = price / days;
Console.WriteLine($"Project duration: {days} days");
Console.WriteLine($"Daily rate: {dailyRate:0.00} USD");
}
TL;DR
DateTimerepresents a moment in time,TimeSpanrepresents a time interval.NowandUtcNowprovide the current time.AddDays,AddMonths, andAddYearsare used for date arithmetic.- The difference between two dates is represented by a
TimeSpan. - Date comparisons can be done with
>,<,==, orCompare. ToStringcan format dates in custom ways.