Date Math
Add, subtract, and compare.
Date Math is a free C# Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Doing Math with Dates
DateTime values are immutable: math never changes the original. Instead, every operation returns a brand new DateTime.
This lesson covers adding and subtracting time, comparing dates, and answering questions like how many days until an event.
AddDays
The AddDays method returns a date that many days later. Pass a negative number to go backward.
Because DateTime is immutable, you must use the returned value; the original stays unchanged.
using System;
class Program
{
static void Main()
{
DateTime d = new DateTime(2024, 3, 5);
DateTime later = d.AddDays(10);
Console.WriteLine(later.ToString("yyyy-MM-dd"));
}
}The Add Family
There is an Add method for each unit: AddHours, AddMinutes, AddSeconds, AddMonths, and AddYears.
They all accept positive or negative numbers, so one set of methods handles both future and past.
using System;
class Program
{
static void Main()
{
DateTime d = new DateTime(2024, 3, 5, 9, 0, 0);
Console.WriteLine(d.AddHours(5).Hour);
}
}AddMonths Handles Overflow
AddMonths is smart about month lengths. Adding one month to January 31 gives February 28 or 29, not an invalid date.
This saves you from manually checking how many days each month has.
using System;
class Program
{
static void Main()
{
DateTime d = new DateTime(2024, 1, 31);
Console.WriteLine(d.AddMonths(1).ToString("yyyy-MM-dd"));
}
}Going Backward
To move into the past, pass a negative argument. AddDays(-7) gives the date one week earlier.
There is no separate Subtract-days method; negative values cover it cleanly.
using System;
class Program
{
static void Main()
{
DateTime d = new DateTime(2024, 3, 5);
Console.WriteLine(d.AddDays(-7).ToString("yyyy-MM-dd"));
}
}Adding a TimeSpan
You can add a TimeSpan directly to a DateTime with the + operator, or with the Add method.
This is convenient when you already have a duration and want to shift a date by it.
using System;
class Program
{
static void Main()
{
DateTime d = new DateTime(2024, 3, 5, 9, 0, 0);
DateTime later = d + TimeSpan.FromHours(3);
Console.WriteLine(later.Hour);
}
}Difference Between Dates
Subtracting one date from another produces a TimeSpan describing the gap.
Read TotalDays to count the days between them, even across months or years.
using System;
class Program
{
static void Main()
{
DateTime start = new DateTime(2024, 1, 1);
DateTime end = new DateTime(2024, 3, 1);
Console.WriteLine((end - start).TotalDays);
}
}Days Until an Event
A common task is counting days until a future date. Subtract today from the target and read TotalDays or Days.
Using fixed dates here keeps the output predictable for learning.
using System;
class Program
{
static void Main()
{
DateTime today = new DateTime(2024, 3, 1);
DateTime party = new DateTime(2024, 3, 15);
Console.WriteLine((party - today).Days);
}
}Comparing Dates
DateTime values compare with <, >, and ==. An earlier date is less than a later one.
There are also methods like CompareTo and Equals when you need a formal comparison result.
using System;
class Program
{
static void Main()
{
DateTime a = new DateTime(2024, 1, 1);
DateTime b = new DateTime(2024, 6, 1);
Console.WriteLine(a < b);
}
}Leap Years and Days in Month
Two static helpers answer calendar questions. DateTime.IsLeapYear(year) returns true for leap years.
DateTime.DaysInMonth(year, month) tells how many days a given month has, handling February correctly.
using System;
class Program
{
static void Main()
{
Console.WriteLine(DateTime.IsLeapYear(2024));
Console.WriteLine(DateTime.DaysInMonth(2024, 2));
}
}Start and End of Day
To get the start of a day, use the Date property, which strips the time to midnight.
For the last moment of a day, add one day and subtract a tick, or add 23:59:59. The Date property is the simplest building block.
using System;
class Program
{
static void Main()
{
DateTime d = new DateTime(2024, 3, 5, 14, 30, 0);
Console.WriteLine(d.Date.ToString("yyyy-MM-dd HH:mm"));
}
}Quick Check
You start with January 31, 2024 and call AddMonths(1). What date do you get?
Recap
DateTime math returns new values. Shift dates with AddDays, AddMonths, and friends, using negatives to go backward.
Subtract dates to get a TimeSpan, compare with operators, and use IsLeapYear, DaysInMonth, and Date for calendar tasks.
Frequently asked questions
Is the “Date Math” lesson free?
Yes — the full text of “Date Math” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “Date Math”?
Add, subtract, and compare. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Date Math” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.