C# Dates and Times
ExampleC#
using System;
class Program
{
static void Main()
{
DateTime d = new DateTime(2026, 11, 4, 14, 30, 0);
Console.WriteLine(d.Year + "-" + d.Month + "-" + d.Day);
Console.WriteLine(d.DayOfWeek);
Console.WriteLine(d.AddDays(30).ToString("yyyy-MM-dd"));
Console.WriteLine(d.ToString("HH:mm"));
DateTime a = new DateTime(2026, 1, 1);
DateTime b = new DateTime(2026, 12, 25);
TimeSpan diff = b - a;
Console.WriteLine(diff.Days);
Console.WriteLine(DateTime.DaysInMonth(2028, 2));
Console.WriteLine(DateTime.IsLeapYear(2026));
}
}Output
2026-11-4 Wednesday 2026-12-04 14:30 358 29 False
Quick check
Which type represents the difference between two DateTime values?
Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.
Tip: use the left and right arrow keys to move between lessons.