Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Date, Time and Globalization

Date arithmetic

Reference for date arithmetic in C#: AddDays, AddMonths and AddYears clamping, subtracting dates into TimeSpan, ages, month ends and DateOnly day counts.

Date arithmetic means moving a date forward or back and measuring the distance between two dates. .NET has two tools. Calendar-based methods (AddDays, AddMonths, AddYears) move a date by calendar units. Duration arithmetic (Add(TimeSpan), subtraction) moves by an exact amount of time.

Every Add... method returns a new value because dates are immutable. Month and year arithmetic has a rule that surprises people: when the target day does not exist, the result is clamped to the last day of the month.

C#
var d = new DateTime(2026, 1, 31);
Console.WriteLine(d.AddMonths(1).ToString("yyyy-MM-dd"));   // 2026-02-28

Try it

C# Adding and subtracting

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Ages, business days and DateOnly counts

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
DateTime AddYears(int value)
DateTime AddMonths(int months)
DateTime AddDays(double value)
DateTime AddHours(double value)
DateTime AddMinutes(double value)
DateTime AddSeconds(double value)
DateTime AddMilliseconds(double value)
DateTime AddTicks(long value)
DateTime Add(TimeSpan value)
TimeSpan operator -(DateTime d1, DateTime d2)
DateTime operator -(DateTime d, TimeSpan t)

DateTimeOffset has the same methods. DateOnly has AddDays, AddMonths and AddYears with int arguments and exposes DayNumber. TimeOnly has Add, AddHours and AddMinutes.

Parameter Description
value, months The amount to add; negative values subtract. AddYears and AddMonths take whole numbers.

Returns: a new date and time. The Kind (or offset) is preserved.

Errors: ArgumentOutOfRangeException when the result is before 0001-01-01 or after 9999-12-31 23:59:59.9999999.

Usage notes

  • Use the result. d.AddDays(1); alone does nothing, because the original does not change.
  • Month and year addition clamps. 31 January + 1 month is 28 (or 29) February, and adding another month gives 28 March, not 31 March. To keep the day, compute from the original: start.AddMonths(2).
  • AddDays(double) accepts fractions. AddDays(1.5) adds 36 hours. DateOnly.AddDays takes an int.
  • Subtracting dates yields a TimeSpan. Use TotalDays for the number of days including the fraction, and Days for the whole-day part. For two DateOnly values, subtract DayNumber.
  • Calendar days are not always 24 hours. In a time zone with daylight saving, a day can be 23 or 25 hours. DateTime arithmetic ignores zones; if the zone matters, convert with TimeZoneInfo, add days to the local clock value, and convert back.
  • Whole years or months between dates are not in the base library. Compute them from the year and month numbers and adjust, as in the age example above.
  • Leap years. AddYears(1) on 29 February gives 28 February of the next year; a person born on 29 February is counted as one year older on 28 February in the age example above.
  • Prefer DateOnly for pure date calculations, to avoid time parts that make comparisons fail.