C# - DateTimeOffset versus DateTime, why use offsets?

DateTime and DatimeTimeOffset are very much alike. They both consist of a date and a time, however a DateTimeOffset also has an offset, which defines an offset compared to Coordinated Universal Time (UTC).

Why is the offset important? Well when persisting a DateTime in a SQL database there is no telling which timezone it was in. With the DateTimeOffset you will have an offset which tells you. As long as a DateTime is an object there will be a DateTime.Kind which tells you whether it is local, UTC or unspecified, but this information is lost upon persistence.

You could have the rule that you store all of your DateTimes as UTC. However I have often found that people tend to forget. Often the rule is that you need to use DateTime.UtcNow instead of DateTime.Now. Easy right? But for some reason this is often forgotten. If you accidentally store a DateTime in LocalTime in your database, it will be a different time if you retrieve it as UTC.

With DateTimeOffset you do not need to worry about this. Even if you use DateTimeOffset.Now you will get an offset (+01:00 CET for example) which can identify an absolute time on earth. If you would still like to store your dates in UTC you can use DateTimeOffset.UtcNow which will give you a DateTimeOffset with the offset of +00:00 (taking into accounting your current local time of course).

The only drawback of DateTimeOffset - as I see it - is that it takes up 2 bytes more when stored in your database.

I hope this helped you, let me know in the comments if it did!