dateTime.ToString() / dateTime.AddDays()
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
// Converts a DateTime to a string using the specified format. string s = dateTime.ToString(string format); // Adds the specified number of days (use a negative value to subtract). DateTime newDateTime = dateTime.AddDays(double value); // Other add methods follow the same pattern. DateTime newDateTime = dateTime.AddHours(double value); DateTime newDateTime = dateTime.AddMinutes(double value); DateTime newDateTime = dateTime.AddMonths(int months); DateTime newDateTime = dateTime.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 in Japanese format (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
using System;
DateTime now = new DateTime(2024, 1, 15, 14, 30, 45);
// Use ToString() with a format string to convert to a string.
Console.WriteLine(now.ToString("yyyy/MM/dd")); // 2024/01/15
Console.WriteLine(now.ToString("yyyy/M/d")); // 2024/1/15
Console.WriteLine(now.ToString("HH:mm:ss")); // 14:30:45
Console.WriteLine(now.ToString("yyyy/MM/dd HH:mm")); // 2024/01/15 14:30
Console.WriteLine(now.ToString("yyyyMMdd")); // 20240115
// You can also use format specifiers in string interpolation.
Console.WriteLine($"{now:yyyy/M/d (ddd)}"); // 2024/1/15 (Mon)
// Use AddDays() to add days.
DateTime dayAfterTomorrow = now.AddDays(2);
Console.WriteLine(dayAfterTomorrow.ToString("yyyy/MM/dd")); // 2024/01/17
// Pass a negative value to subtract days.
DateTime yesterday = now.AddDays(-1);
Console.WriteLine(yesterday.ToString("yyyy/MM/dd")); // 2024/01/14
// You can also add months, years, and hours.
Console.WriteLine(now.AddMonths(3).ToString("yyyy/MM/dd")); // 2024/04/15
Console.WriteLine(now.AddYears(1).ToString("yyyy/MM/dd")); // 2025/01/15
Console.WriteLine(now.AddHours(8).ToString("HH:mm")); // 22:30
Notes
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.