C# – Get the current date and time

Here’s an example of how to get the current date/time:

var now = DateTime.Now; Console.WriteLine($"Current local time: {now}");
Code language: C# (cs)

This outputs the current local date/time:

Current local time: 6/21/2022 3:45:42 PM
Code language: plaintext (plaintext)

Note: By default, it uses the current culture’s format (from the OS). This is showing the US date format – MM/dd/yyyy.

DateTime.Now is the local date/time from the system where the code is executing. Keep that in mind if you’re getting unexpected date/times. The first thing to check is the system’s date/time configuration (including the system’s time zone config).

If you’re passing date/times strings around, it’s typically a great idea to use UTC or include the UTC offset. Here’s an example of getting the current UTC time, getting the local time with a UTC offset, and formatting DateTime.Now to include the UTC offset:

var utcNow = DateTime.UtcNow; Console.WriteLine($"Current UTC time: {utcNow}"); var nowWithOffset = DateTimeOffset.Now; Console.WriteLine($"Current time with offset: {nowWithOffset}"); var iso8601format = DateTime.Now.ToString("o"); Console.WriteLine($"Current time with ISO-8601 format: {iso8601format}");
Code language: C# (cs)

This outputs the following:

Current UTC time: 6/21/2022 7:57:48 PM Current time with offset: 6/21/2022 3:57:48 PM -04:00 Current time with ISO-8601 format: 2022-06-21T15:57:48.0907270-04:00
Code language: plaintext (plaintext)

It uses the time zone configuration from the system to get the current UTC offset (-04:00 in my case), and then uses this to figure out the current UTC time. To repeat, all of this is based on the system’s date/time configuration.

Note: If a time zone observes daylight savings time, the UTC offset changes based on the time of year. So don’t try to hardcode the offset.

Format it

You can format the DateTime (or DateTimeOffset) object however you want. Here’s an example of using custom formats:

var date = DateTime.Now.ToString("yyyy-M-d"); Console.WriteLine($"Current date: {date}"); var time = DateTime.Now.ToString("h:mm tt"); Console.WriteLine($"Current time: {time}");
Code language: C# (cs)

Note: When you start to type in the format string, IntelliSense will show a full list of format specifiers to help you. You can also take a look at the official MSDN documentation.

This outputs the following:

Current date: 2022-6-21 Current time: 4:29 PM
Code language: plaintext (plaintext)

There are also several helper methods for common formats. Here’s an example of using two helper methods:

var date = DateTime.Now.ToShortDateString(); Console.WriteLine($"Current date: {date}"); var time = DateTime.Now.ToShortTimeString(); Console.WriteLine($"Current time: {time}");
Code language: C# (cs)

This outputs the following:

Current date: 6/21/2022 Current time: 4:28 PM
Code language: plaintext (plaintext)

Get just the time or just the date

Here’s an example of getting just the time component of the current date/time:

TimeSpan time = DateTime.Now.TimeOfDay; Console.WriteLine($"Clock hours (24-h): {time.Hours}"); Console.WriteLine($"Hours since noon: {time.Hours - 12}");
Code language: C# (cs)

This outputs:

Clock hours (24-h): 16 Hours since noon: 4
Code language: plaintext (plaintext)

You can’t really get “just the date”, but you can get the current date with the time zeroed out (set to 00:00:00), which allows you to compare it with other dates with the time zeroed out (such as dates from the database). Here’s an example:

var currentDate = DateTime.Now.Date; Console.WriteLine(currentDate); var futureDate = new DateTime(year: 2022, month: 12, day: 21); Console.WriteLine($"Difference = {(futureDate - currentDate).TotalDays}");
Code language: C# (cs)

This outputs the following:

6/21/2022 12:00:00 AM Difference = 183
Code language: plaintext (plaintext)

If you had just used DateTime.Now and compared it with the 2022-12-21, it would have fractional days (182.xx) because they have different times. This is the reason for zeroing out the time, so that you can get whole day (or month/year) differences.

Note: In .NET 6, they added DateOnly and TimeOnly to try to simplify getting just the date or just the time. I’ll show the downside to those further below.

Get current time for a specific time zone

You can use the TimeZoneInfo class to convert your local time to a specified time zone. Here’s an example:

var localNow = DateTimeOffset.Now; var usCentralTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time"); var centralTimeNow = TimeZoneInfo.ConvertTime(localNow, usCentralTimeZone); Console.WriteLine(centralTimeNow);
Code language: C# (cs)

This outputs the following:

6/21/2022 2:26:19 PM -05:00
Code language: plaintext (plaintext)

If you don’t want the TimeZoneInfo object (from FindSystemZoneById()), you can use the shortcut approach:

var centralTimeNow = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTimeOffset.Now, "Central Standard Time");
Code language: C# (cs)

Find all time zone ids

If you’re not sure about the time zone id you want, you can find all time zone ids on your system like this:

foreach (var timezone in TimeZoneInfo.GetSystemTimeZones()) { Console.WriteLine(timezone.Id); }
Code language: C# (cs)

On my system, this output about 150 time zone ids (too many to show here).

What about DateOnly and TimeOnly (.NET 6)?

In .NET 6, they added the DateOnly and TimeOnly types. In general, I wouldn’t suggest using these, because they don’t currently work with things such as the built-in JSON serializer. Here’s an example:

var person = new Person() { DateOfBirth = DateOnly.FromDateTime(DateTime.Now) }; Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(person));
Code language: C# (cs)

This throws the following exception, indicating that it can’t serialize a DateOnly property:

System.NotSupportedException: Serialization and deserialization of ‘System.DateOnly’ instances are not supported. Path: $.DateOfBirth.

Keep it simple and stick with DateTime, DateTimeOffset, and TimeSpan instead.

Leave a Comment