C# Date and Time
Date and Time
Many programs work with dates. A calendar shows today's date, a shop prints the date on a receipt, and a game counts the days until a new level opens.
C# has a built-in type for this: DateTime. A DateTime stores a date and a time together.
To create a date, use new DateTime() with the year, the month and the day:
Example
DateTime myDate = new DateTime(2025, 3, 14);
Console.WriteLine(myDate); // 3/14/2025 12:00:00 AM
When you don't give a time, the time is set to midnight (12:00:00 AM).
To add a time, write the hour, the minute and the second after the date:
Example
DateTime meeting = new DateTime(2025, 3, 14, 15, 30, 0);
Console.WriteLine(meeting); // 3/14/2025 3:30:00 PM
The hour goes from 0 to 23, so 15 is 3 PM.
Get Parts of a Date
A DateTime has properties for each part of the date:
Example
DateTime myDate = new DateTime(2025, 3, 14);
Console.WriteLine(myDate.Year); // 2025
Console.WriteLine(myDate.Month); // 3
Console.WriteLine(myDate.Day); // 14
Console.WriteLine(myDate.DayOfWeek); // Friday
DayOfWeek gives the name of the day. C# works it out for you.
Current Date and Time
DateTime.Now gives the current date and time of the computer.
DateTime.Today gives today's date, with the time set to midnight:
Example
Console.WriteLine(DateTime.Now);
Console.WriteLine(DateTime.Today);
The output is different every time you run the program.
Format a Date
To choose how a date looks, use ToString() with a format. The letters in the format are replaced with parts of the date:
Example
DateTime myDate = new DateTime(2025, 3, 14, 15, 30, 0);
Console.WriteLine(myDate.ToString("yyyy-MM-dd")); // 2025-03-14
Console.WriteLine(myDate.ToString("HH:mm")); // 15:30
Console.WriteLine(myDate.ToString("dddd dd MMMM yyyy")); // Friday 14 March 2025
Other characters in the format, like - and spaces, are printed as they are.
These are the most common format letters:
| Letters | Meaning | Example |
|---|---|---|
yyyy | Year | 2025 |
MM | Month (2 digits) | 03 |
MMMM | Name of the month | March |
dd | Day (2 digits) | 14 |
dddd | Name of the day | Friday |
HH | Hour (00 to 23) | 15 |
mm | Minutes | 30 |
ss | Seconds | 00 |
Add Days, Months and Years
AddDays() adds a number of days to a date, and returns the new date:
Example
DateTime start = new DateTime(2025, 3, 14);
DateTime end = start.AddDays(20);
Console.WriteLine(end.ToString("yyyy-MM-dd")); // 2025-04-03
March has 31 days, so 20 days after March 14 is April 3. C# knows how many days each month has.
AddMonths() adds months, and AddYears() adds years. Use a negative number to go back in time.
In this example, ToString() is called directly on each new date:
Example
DateTime myDate = new DateTime(2025, 3, 14);
Console.WriteLine(myDate.AddMonths(2).ToString("yyyy-MM-dd")); // 2025-05-14
Console.WriteLine(myDate.AddYears(1).ToString("yyyy-MM-dd")); // 2026-03-14
Console.WriteLine(myDate.AddDays(-7).ToString("yyyy-MM-dd")); // 2025-03-07
Days Between Two Dates
When you subtract one date from another, you get a TimeSpan. A TimeSpan stores a length of time.
Its Days property gives the number of whole days:
Example
DateTime today = new DateTime(2025, 3, 14);
DateTime birthday = new DateTime(2025, 7, 1);
TimeSpan wait = birthday - today;
Console.WriteLine(wait.Days); // 109
Compare Dates
You can compare two dates with the comparison operators, like < and >. An earlier date is smaller than a later date:
Example
DateTime deadline = new DateTime(2025, 3, 31);
DateTime today = new DateTime(2025, 3, 14);
if (today < deadline)
{
Console.WriteLine("You still have time.");
}
else
{
Console.WriteLine("Too late!");
}
March 14 is before March 31, so today < deadline is true.