dateTime.ToString() / dateTime.AddDays()
| Since: | C# 1.0(2002) |
|---|
Methods for converting a date/time value to a formatted string using ToString(), and for adding or subtracting days, hours, and other units using AddDays() and related methods.
Syntax
string s = dt.ToString(string format); // Adds the specified number of days (use a negative value to subtract). DateTime newDt = dt.AddDays(double value); // Other add methods follow the same pattern. DateTime newDt = dt.AddHours(double value); DateTime newDt = dt.AddMinutes(double value); DateTime newDt = dt.AddMonths(int months); DateTime newDt = dt.AddYears(int years);
Method List
| Method | Description |
|---|---|
| ToString("yyyy/MM/dd") | Outputs the date with / separators (e.g., 2024/01/15). |
| ToString("yyyy/M/d") | Outputs the date without zero-padding (e.g., 2024/1/15). |
| ToString("HH:mm:ss") | Outputs the time in 24-hour format (e.g., 14:30:00). |
| AddDays(n) | Returns a DateTime that is n days in the future. Pass a negative value to go n days into the past. The original DateTime is not modified. |
| AddMonths(n) | Returns a DateTime that is n months in the future. |
| AddYears(n) | Returns a DateTime that is n years in the future. |
| AddHours(n) | Returns a DateTime that is n hours in the future. |
Sample Code
Program.cs
using System;
DateTime dt = new DateTime(2024, 1, 15, 14, 30, 45);
// Use ToString() with a format string to convert to a string.
Console.WriteLine(dt.ToString("yyyy/MM/dd")); // 2024/01/15
Console.WriteLine(dt.ToString("yyyy/M/d")); // 2024/1/15
Console.WriteLine(dt.ToString("HH:mm:ss")); // 14:30:45
Console.WriteLine(dt.ToString("yyyy/MM/dd HH:mm")); // 2024/01/15 14:30
Console.WriteLine(dt.ToString("yyyyMMdd")); // 20240115
// You can also use format specifiers in string interpolation.
Console.WriteLine($"{dt:yyyy/M/d (ddd)}"); // 2024/1/15 (Mon)
// Use AddDays() to add days.
DateTime dayAfterTomorrow = dt.AddDays(2);
Console.WriteLine(dayAfterTomorrow.ToString("yyyy/MM/dd")); // 2024/01/17
// Pass a negative value to subtract days.
DateTime yesterday = dt.AddDays(-1);
Console.WriteLine(yesterday.ToString("yyyy/MM/dd")); // 2024/01/14
// You can also add months, years, and hours.
Console.WriteLine(dt.AddMonths(3).ToString("yyyy/MM/dd")); // 2024/04/15
Console.WriteLine(dt.AddYears(1).ToString("yyyy/MM/dd")); // 2025/01/15
Console.WriteLine(dt.AddHours(8).ToString("HH:mm")); // 22:30
The following example demonstrates this:
dotnet script datetime_tostring_adddays.csx 2024/01/15 2024/1/15 14:30:45 2024/01/15 14:30 20240115 2024/1/15 (Mon) 2024/01/17 2024/01/14 2024/04/15 2025/01/15 22:30
Practical Pattern: Schedule Calculation
A pattern for calculating deadlines and event dates from a base date. This example uses Kurisu Makise's paper submission timeline.
Schedule.cs
using System;
// Sets the base date (paper submission date).
DateTime submissionDate = new DateTime(2024, 6, 1);
// Calculates the review deadline 14 days later.
DateTime reviewDeadline = submissionDate.AddDays(14);
Console.WriteLine($"Review deadline: {reviewDeadline:yyyy/MM/dd}");
// Calculates the publication date 3 months later.
DateTime publishDate = submissionDate.AddMonths(3);
Console.WriteLine($"Publication date: {publishDate:yyyy/MM/dd}");
// Calculates the number of days remaining until the deadline.
TimeSpan remaining = reviewDeadline - DateTime.Today;
Console.WriteLine($"Days until review deadline: {(int)remaining.TotalDays}");
// Generates a reminder date one day before the deadline.
DateTime reminderDay = reviewDeadline.AddDays(-1);
Console.WriteLine($"Reminder: {reminderDay:yyyy/MM/dd (ddd)}");
Run the following command:
dotnet script schedule.csx Review deadline: 2024/06/15 Publication date: 2024/09/01 Days until review deadline: 75 Reminder: 2024/06/14 (Fri)
Practical Pattern: Format Pattern Reference
A reference of commonly used format patterns.
FormatPatterns.cs
using System;
DateTime dt = new DateTime(2024, 1, 15, 8, 5, 9);
// Date formats
Console.WriteLine(dt.ToString("yyyy/MM/dd")); // 2024/01/15 (zero-padded)
Console.WriteLine(dt.ToString("yyyy/M/d")); // 2024/1/15 (no padding)
Console.WriteLine(dt.ToString("yyyyMMdd")); // 20240115 (for filenames)
Console.WriteLine(dt.ToString("dddd, MMMM d")); // Monday, January 15
// Time formats
Console.WriteLine(dt.ToString("HH:mm:ss")); // 08:05:09 (24-hour, zero-padded)
Console.WriteLine(dt.ToString("H:mm")); // 8:05 (24-hour, no padding)
Console.WriteLine(dt.ToString("hh:mm tt")); // 08:05 AM (12-hour)
// Date + time
Console.WriteLine(dt.ToString("yyyy-MM-dd HH:mm:ss")); // 2024-01-15 08:05:09
Console.WriteLine(dt.ToString("o")); // ISO 8601 format
// Standard format specifiers
Console.WriteLine(dt.ToString("d")); // 1/15/2024 (short date)
Console.WriteLine(dt.ToString("D")); // Monday, January 15, 2024 (long date)
Console.WriteLine(dt.ToString("t")); // 8:05 AM (short time)
Console.WriteLine(dt.ToString("f")); // Monday, January 15, 2024 8:05 AM
Run the following command:
dotnet script format_patterns.csx 2024/01/15 2024/1/15 20240115 Monday, January 15 08:05:09 8:05 08:05 AM 2024-01-15 08:05:09 2024-01-15T08:05:09.0000000 1/15/2024 Monday, January 15, 2024 8:05 AM Monday, January 15, 2024 8:05 AM
Common Mistakes
Common Mistake: Confusing M (month) with m (minutes) and H (24-hour) with h (12-hour)
Format string specifiers are case-sensitive. In particular, M and m are easy to mix up and produce unexpected output.
using System;
DateTime dt = new DateTime(2024, 3, 15, 14, 30, 0);
// NG: Confusing M (month) with m (minutes)
Console.WriteLine(dt.ToString("yyyy/mm/dd")); // yyyy/30/15 ← minutes instead of month!
// OK: Use M (uppercase) for month
Console.WriteLine(dt.ToString("yyyy/MM/dd")); // 2024/03/15
// NG: Confusing HH (24-hour) with hh (12-hour)
Console.WriteLine(dt.ToString("hh:mm:ss")); // 02:30:00 (12-hour format)
// OK: Use HH (uppercase) for 24-hour format
Console.WriteLine(dt.ToString("HH:mm:ss")); // 14:30:00
Run the following command:
dotnet run yyyy/30/15 2024/03/15 02:30:00 14:30:00
Overview
Methods like AddDays() do not modify the original DateTime value. They return a new DateTime as the result, so always assign the return value to a variable.
In format strings, the basic specifiers are yyyy (4-digit year), MM (2-digit month), dd (2-digit day), and HH (24-hour hour). Note that M (uppercase) means month while m (lowercase) means minutes — mixing up the case will produce unexpected output.
To calculate the difference between two date/time values, see TimeSpan / Date Difference.
If you find any errors or copyright issues, please contact us.