DateTime

Almost every program eventually needs to work with dates and times — timestamps, countdowns, "how long ago", scheduling. C#'s DateTime type handles all of it: creating dates, formatting them for humans, doing arithmetic, measuring durations with TimeSpan , and safely parsing dates from text. Get comfortable here and a whole category of tasks becomes easy.

Learn DateTime in our free C# course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick recall.

Part of the free C# course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

Think of a DateTime as a precise pin stuck into a calendar — a single instant. A TimeSpan , by contrast, is the distance between two pins — "three days and four hours" — with no fixed place on the calendar. Subtract one pin from another and you get the gap (a TimeSpan ); add a gap to a pin and you land on a new pin (a DateTime ). Keeping "an instant" and "a duration" separate in your head is the key to working with time.

1. Creating & Reading Dates

DateTime.Now , DateTime.Today and DateTime.UtcNow give you "right now" in different forms, while the constructor builds a specific date from year/month/day (and optionally a time). Once you have a date, you can read parts like .Year , .Month and .DayOfWeek .

2. Formatting Dates

Humans want dates in many shapes. Standard format specifiers ( "d" , "D" , "t" ) give common presets, while custom strings ( "yyyy-MM-dd" , "dd MMM yyyy" ) let you build any layout. Pass CultureInfo.InvariantCulture when you need stable, predictable output regardless of the machine's region.

3. Date Arithmetic & TimeSpan

DateTime is immutable, so AddDays , AddHours and friends return a new date. Subtracting two dates gives a TimeSpan — a duration you can read as .TotalDays , .Days , .Hours , and so on. Comparison operators ( > , < ) work directly on dates too.

Your turn — measure a tenure and compute the next review date.

4. Parsing Dates from Text

Dates often arrive as strings — from files, forms, or APIs. Parse throws on bad input, so for anything from a user prefer TryParse , which returns false instead of crashing. When you know the exact incoming format, ParseExact pins it down precisely.

These lines compute how many days are left until a deadline, but they're scrambled. Order them so the program compiles and prints the day count.

Both dates must exist before you subtract them, and the TimeSpan must exist before you read .Days .

15 — AddDays returns a new date on the 15th; d itself is unchanged.

7 — the TimeSpan between the two dates is 7 days.

2025-03-09 — the custom format produces an ISO-style date.

A "days until" countdown ties together creation, formatting, arithmetic, TimeSpan and DayOfWeek — the bread and butter of date code.

Compute someone's age in whole years from a birth date — and handle the "haven't had this year's birthday yet" edge case correctly. A classic interview-style date problem.

Practice quiz

What is the difference between DateTime.Now and DateTime.UtcNow?

  • They are identical
  • UtcNow includes the date but Now does not
  • Now is the machine's local time; UtcNow is the current time in Coordinated Universal Time (UTC)
  • Now is always one hour ahead of UtcNow

Answer: Now is the machine's local time; UtcNow is the current time in Coordinated Universal Time (UTC). DateTime.Now is local time (depends on the machine's time zone), while DateTime.UtcNow is UTC — preferred for storing and comparing timestamps.

What does DateTime.Today return?

  • Midnight today (the date with no time-of-day part)
  • The current time including hours and minutes
  • Tomorrow's date
  • The date in UTC

Answer: Midnight today (the date with no time-of-day part). DateTime.Today returns today's date at midnight, with no time-of-day component.

Is DateTime mutable? What do methods like AddDays do?

  • DateTime is mutable; AddDays changes the original in place
  • AddDays only works on DateTime.Now
  • AddDays throws if the result is in the future
  • DateTime is immutable; AddDays returns a NEW DateTime and leaves the original unchanged

Answer: DateTime is immutable; AddDays returns a NEW DateTime and leaves the original unchanged. DateTime is immutable. AddDays, AddHours, AddYears, etc. return a new DateTime, so you must assign the result to a variable.

What do you get when you subtract one DateTime from another?

  • Another DateTime
  • A TimeSpan representing the duration between them
  • An int number of days
  • A string

Answer: A TimeSpan representing the duration between them. Subtracting two DateTimes yields a TimeSpan — a duration you can read as .TotalDays, .Days, .Hours, and so on.

What does a TimeSpan represent?

  • A duration — a length of time, not a point in time
  • A single instant on the calendar
  • A time zone
  • A formatted date string

Answer: A duration — a length of time, not a point in time. A TimeSpan is a duration (e.g. 'three days and four hours'), with no fixed place on the calendar — unlike a DateTime, which is an instant.

Why pass CultureInfo.InvariantCulture when formatting or parsing dates?

  • It makes formatting faster
  • It is required for all DateTime methods
  • For predictable, stable output that doesn't change with the machine's region
  • It converts the date to UTC

Answer: For predictable, stable output that doesn't change with the machine's region. By default ToString uses the machine's culture, so output varies by region. InvariantCulture gives stable, predictable results.

When should you prefer DateTime.TryParse over DateTime.Parse?

  • Always — Parse is deprecated
  • For input from users or external sources, because TryParse returns false on bad input instead of throwing
  • Only when parsing UTC dates
  • Never — they behave identically

Answer: For input from users or external sources, because TryParse returns false on bad input instead of throwing. Parse throws on bad input. TryParse returns false instead of crashing, so it's the safe choice for user or external input.

What does DateTime.ParseExact let you do?

  • Parse any format automatically
  • Always parse in UTC
  • Round the time to the nearest hour
  • Pin down the exact expected format, e.g. ParseExact("25/12/2025", "dd/MM/yyyy", ci)

Answer: Pin down the exact expected format, e.g. ParseExact("25/12/2025", "dd/MM/yyyy", ci). ParseExact requires the input to match a specified format string exactly, useful when you know the precise incoming format.

Which custom format string produces an ISO-style date like 2025-03-09?

  • "dd MMM yyyy"
  • "yyyy-MM-dd"
  • "D"
  • "t"

Answer: "yyyy-MM-dd". The custom format 'yyyy-MM-dd' produces an ISO-style date such as 2025-03-09.

Which property tells you the day of the week for a DateTime?

  • .Weekday
  • .Day
  • .DayOfWeek
  • .DayName

Answer: .DayOfWeek. DateTime.DayOfWeek returns a DayOfWeek value (e.g. Thursday), letting you check whether a date falls on a weekend.