Code4IT

The place for .NET enthusiasts, Azure lovers, and backend developers

C# tip: create correct DateTimes with DateTimeKind

2021-08-07 2 min read CSharp Tips
Just a second! 🫷
If you are here, it means that you are a software developer. So, you know that storage, networking, and domain management have a cost .

If you want to support this blog, please ensure that you have disabled the adblocker for this site. I configured Google AdSense to show as few ADS as possible - I don't want to bother you with lots of ads, but I still need to add some to pay for the resources for my site.

Thank you for your understanding.
- Davide

One of the most common issues we face when developing applications is handling dates, times, and time zones.

Let’s say that we need the date for January 1st, 2020, exactly 30 minutes after midnight. We would be tempted to do something like:

var plainDate = new DateTime(2020, 1, 1, 0, 30, 0);

It makes sense. And plainDate.ToString() returns 2020/1/1 0:30:00, which is correct.

But, as I explained in a previous article, while ToString does not care about time zone, when you use ToUniversalTime and ToLocalTime, the results differ, according to your time zone.

Let’s use a real example. Please, note that I live in UTC+1, so pay attention to what happens to the hour!

var plainDate = new DateTime(2020, 1, 1, 0, 30, 0);

Console.WriteLine(plainDate);  // 2020-01-01 00:30:00
Console.WriteLine(plainDate.ToUniversalTime());  // 2019-12-31 23:30:00
Console.WriteLine(plainDate.ToLocalTime());  // 2020-01-01 01:30:00

This means that ToUniversalTime considers plainDate as Local, so, in my case, it subtracts 1 hour. On the contrary, ToLocalTime considers plainDate as UTC, so it adds one hour.

So what to do?

Always specify the DateTimeKind parameter when creating DateTimes__. This helps the application understanding which kind of date is it managing.

var specificDate = new DateTime(2020, 1, 1, 0, 30, 0, DateTimeKind.Utc);

Console.WriteLine(specificDate); //2020-01-01 00:30:00
Console.WriteLine(specificDate.ToUniversalTime()); //2020-01-01 00:30:00
Console.WriteLine(specificDate.ToLocalTime()); //2020-01-01 00:30:00

As you see, it’s always the same date.

Ah, right! DateTimeKind has only 3 possible values:

public enum DateTimeKind
{
    Unspecified,
    Utc,
    Local
}

So, my suggestion is to always specify the DateTimeKind parameter when creating a new DateTime.

If you want to know more about Time and Timezones, I’d suggest watching this YouTube video by Computerphile.

πŸ‘‰ Let’s discuss it on Twitter or on the comment section below.

🐧

About the author

Davide Bellone is a Principal Backend Developer with more than 10 years of professional experience with Microsoft platforms and frameworks.

He loves learning new things and sharing these learnings with others: that’s why he writes on this blog and is involved as speaker at tech conferences.

He's a Microsoft MVP πŸ†, conference speaker (here's his Sessionize Profile) and content creator on LinkedIn.