DateTime.Now / DateTime.Today
| Since: | C# 1.0(2002) |
|---|
DateTime.Now retrieves the current date and time, and DateTime.Today retrieves today's date with the time set to 00:00:00.
Syntax
DateTime dt = DateTime.Now; // Gets today's date (time is 00:00:00). DateTime dt = DateTime.Today; DateTime dt = DateTime.UtcNow;
Member List
| Member | Description |
|---|---|
| DateTime.Now | Returns the current local date and time (year, month, day, hour, minute, second). |
| DateTime.Today | Returns today's date. The time portion is set to 00:00:00. |
| DateTime.UtcNow | Returns the current UTC date and time. Used for server-side processing and logging. |
| .Year / .Month / .Day | Gets the year, month, or day as an integer. |
| .Hour / .Minute / .Second | Gets the hour, minute, or second as an integer. |
| .DayOfWeek | Returns the day of the week as a DayOfWeek enum value (e.g., DayOfWeek.Monday). |
Sample Code
Program.cs
using System;
DateTime now = DateTime.Now;
Console.WriteLine(now); // e.g., 1/15/2024 2:30:45 PM
// Gets today's date (no time component).
DateTime today = DateTime.Today;
Console.WriteLine(today); // e.g., 1/15/2024 12:00:00 AM
// Gets each component individually.
Console.WriteLine($"Year: {now.Year}");
Console.WriteLine($"Month: {now.Month}");
Console.WriteLine($"Day: {now.Day}");
Console.WriteLine($"Hour: {now.Hour}");
Console.WriteLine($"Minute: {now.Minute}");
Console.WriteLine($"Second: {now.Second}");
Console.WriteLine($"Day of week: {now.DayOfWeek}");
// Displays the day of the week as a short name.
string[] dayNames = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Console.WriteLine($"Day of week (short): {dayNames[(int)now.DayOfWeek]}");
DateTime utc = DateTime.UtcNow;
Console.WriteLine($"UTC: {utc}");
Run the following command:
dotnet script datetime_now_today.csx 1/15/2024 2:30:45 PM 1/15/2024 12:00:00 AM Year: 2024 Month: 1 Day: 15 Hour: 14 Minute: 30 Second: 45 Day of week: Monday Day of week (short): Mon UTC: 1/15/2024 5:30:45 AM
Practical Pattern: Date Comparison and Checks
DateTime values can be compared directly using comparison operators (<, >, ==). This is useful for birthday checks and expiration date validation.
DateComparison.cs
using System;
// Birthday: Rintaro Okabe (born December 14, 2000, for this example)
DateTime birthday = new DateTime(2000, 12, 14);
DateTime today = DateTime.Today;
bool isBirthday = today.Month == birthday.Month && today.Day == birthday.Day;
Console.WriteLine($"Today is a birthday: {isBirthday}");
// Calculates age.
int age = today.Year - birthday.Year;
if (today < birthday.AddYears(age))
age--;
Console.WriteLine($"Age: {age}");
// Expiration check (is the expiry date before today?).
DateTime expiry = new DateTime(2024, 3, 31);
bool isExpired = expiry < today;
Console.WriteLine($"Expired: {isExpired}");
// Uses MinValue as a sentinel for "not set".
DateTime lastLogin = DateTime.MinValue;
if (lastLogin == DateTime.MinValue)
Console.WriteLine("Never logged in.");
Run the following command:
dotnet script date_comparison.csx Today is a birthday: False Age: 23 Expired: True Never logged in.
Practical Pattern: Time Zone Handling
On the server side, the standard practice is to store dates in UTC and convert to local time only for display.
TimeZoneExample.cs
using System;
// Use UtcNow for logging and database storage.
DateTime utcNow = DateTime.UtcNow;
Console.WriteLine($"UTC (for storage): {utcNow:yyyy-MM-dd HH:mm:ss}");
// Convert to JST (UTC+9) for display.
TimeZoneInfo jst = TimeZoneInfo.FindSystemTimeZoneById("Tokyo Standard Time");
DateTime jstNow = TimeZoneInfo.ConvertTimeFromUtc(utcNow, jst);
Console.WriteLine($"JST (for display): {jstNow:yyyy-MM-dd HH:mm:ss}");
// DateTimeOffset includes time zone offset information.
DateTimeOffset now = DateTimeOffset.UtcNow;
DateTimeOffset jstOffset = now.ToOffset(TimeSpan.FromHours(9));
Console.WriteLine($"DateTimeOffset JST: {jstOffset:yyyy-MM-dd HH:mm:ss zzz}");
Run the following command:
dotnet script timezone_example.csx UTC (for storage): 2024-01-15 05:30:45 JST (for display): 2024-01-15 14:30:45 DateTimeOffset JST: 2024-01-15 14:30:45 +09:00
Common Mistakes
Common Mistake 1: Calling DateTime.Now Multiple Times
DateTime.Now retrieves the current time each time it is called. Calling it multiple times introduces slight time differences between values, so store it in a variable first.
using System;
// NG: Calling it twice means "start" and "done" have slightly different times.
Console.WriteLine($"Start: {DateTime.Now}");
// ...some processing...
Console.WriteLine($"Done: {DateTime.Now}");
// OK: Store the value in a variable and reuse it.
DateTime start = DateTime.Now;
Console.WriteLine($"Start: {start}");
// ...some processing...
Console.WriteLine($"Done: {start}"); // Displays the same time.
Common Mistake 2: Confusing DateTime.Today with DateTime.Now
When comparing by date only, using DateTime.Now includes the time component, which can cause "same day" comparisons to fail.
using System;
DateTime targetDate = new DateTime(2024, 1, 15); // No time component
// NG: DateTime.Now includes a time component, so == comparison fails unless the time is exactly 00:00:00.
bool wrong = DateTime.Now == targetDate;
// OK: Use DateTime.Today for date-only comparison.
bool correct = DateTime.Today == targetDate;
Console.WriteLine($"Today: {correct}");
// Alternatively, strip the time using the .Date property.
bool alsoCorrect = DateTime.Now.Date == targetDate;
Console.WriteLine($"Now.Date: {alsoCorrect}");
Overview
Both DateTime.Now and DateTime.Today are static properties. Since they retrieve the current time each time they are called, store the value in a variable if you need to use it multiple times within the same operation.
For logging and database storage, use DateTime.UtcNow, which is not affected by time zone differences. To format a date and time as a string, see DateTime.ToString() / DateTime.AddDays().
If you find any errors or copyright issues, please contact us.