Dates, Times, and Formatting
Date Arithmetic
Add days to a fixed date and compare the result.
dates
Date methods return new values, so arithmetic can be assigned and formatted.
Date Arithmetic
DateArithmetic.cs
Replay: real traced execution (multi-file project)
using System;
using System.Globalization;
class Program
{
static void Main()
{
int addDays = 3;
DateTime start = new DateTime(2026, 4, 10);
DateTime due = start.AddDays(addDays);
bool sameMonth = start.Month == due.Month;
string dueText = due.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
Console.WriteLine($"due={dueText}");
Console.WriteLine($"sameMonth={sameMonth}");
}
}
using System;
using System.Globalization;
class Program
{
static void Main()
{
int addDays = 1;
DateTime start = new DateTime(2026, 4, 10);
DateTime due = start.AddDays(addDays);
bool sameMonth = start.Month == due.Month;
string dueText = due.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
Console.WriteLine($"due={dueText}");
Console.WriteLine($"sameMonth={sameMonth}");
}
}
using System;
using System.Globalization;
class Program
{
static void Main()
{
int addDays = 7;
DateTime start = new DateTime(2026, 4, 10);
DateTime due = start.AddDays(addDays);
bool sameMonth = start.Month == due.Month;
string dueText = due.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
Console.WriteLine($"due={dueText}");
Console.WriteLine($"sameMonth={sameMonth}");
}
}
addDays ← 3, start ← 04/10/2026 00:00:00, due ← 04/13/2026 00:00:00
5{6 static void Main()7 {8 int addDays→ 3 = 3; //@addDays=1, 79 DateTime start→ 04/10/2026 00:00:00 = new DateTime(2026, 4, 10);10 DateTime due→ 04/13/2026 00:00:00 = start04/10/2026 00:00:00.AddDays(addDays3);11 bool sameMonth→ True = start.Month4 == due.Month4;1213 string dueText→ 2026-04-13 = due04/13/2026 00:00:00.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);1415 Console.WriteLine($"due={dueText2026-04-13}");16 Console.WriteLine($"sameMonth={sameMonthTrue}");17 }outputdue=2026-04-13 sameMonth=True
addDays ← 1, start ← 04/10/2026 00:00:00, due ← 04/11/2026 00:00:00
5{6 static void Main()7 {8 int addDays→ 1 = 1;9 DateTime start→ 04/10/2026 00:00:00 = new DateTime(2026, 4, 10);10 DateTime due→ 04/11/2026 00:00:00 = start04/10/2026 00:00:00.AddDays(addDays1);11 bool sameMonth→ True = start.Month4 == due.Month4;1213 string dueText→ 2026-04-11 = due04/11/2026 00:00:00.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);1415 Console.WriteLine($"due={dueText2026-04-11}");16 Console.WriteLine($"sameMonth={sameMonthTrue}");17 }outputdue=2026-04-11 sameMonth=True
addDays ← 7, start ← 04/10/2026 00:00:00, due ← 04/17/2026 00:00:00
5{6 static void Main()7 {8 int addDays→ 7 = 7;9 DateTime start→ 04/10/2026 00:00:00 = new DateTime(2026, 4, 10);10 DateTime due→ 04/17/2026 00:00:00 = start04/10/2026 00:00:00.AddDays(addDays7);11 bool sameMonth→ True = start.Month4 == due.Month4;1213 string dueText→ 2026-04-17 = due04/17/2026 00:00:00.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);1415 Console.WriteLine($"due={dueText2026-04-17}");16 Console.WriteLine($"sameMonth={sameMonthTrue}");17 }outputdue=2026-04-17 sameMonth=True