Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between System.DateTime and System.DateTimeOffset

Can anyone explain the difference between System.DateTime and System.DateTimeOffset in C#.NET? Which is best suited for building web apps with users from different time zones?

like image 583
Samuel Liew Avatar asked Jul 01 '11 08:07

Samuel Liew


People also ask

What is the difference between DateTimeOffset and DateTime?

With its Kind property, DateTime is able to reflect only Coordinated Universal Time (UTC) and the system's local time zone. DateTimeOffset reflects a time's offset from UTC, but it doesn't reflect the actual time zone to which that offset belongs.

Is DateTimeOffset better than DateTime?

These uses for DateTimeOffset values are much more common than those for DateTime values. As a result, consider DateTimeOffset as the default date and time type for application development. A DateTimeOffset value isn't tied to a particular time zone, but can originate from a variety of time zones.

What is System DateTimeOffset?

The DateTimeOffset structure includes a DateTime value, together with an Offset property that defines the difference between the current DateTimeOffset instance's date and time and Coordinated Universal Time (UTC).

When should I use DateTimeOffset?

If you need to know when things actually occurred, with more precision than just the approximate date, and you can't be 100% sure that your dates are ALWAYS stored in UTC, then you should consider using DateTimeOffset to represent your datetime values.


3 Answers

There are a couple of point here:

DateTime information should be stored in UTC format in your database:

https://web.archive.org/web/20201202215446/http://www.4guysfromrolla.com/articles/081507-1.aspx

When you use DateTime information in your Web Application you will need to convert it to LocalTime:

 DateTime.UtcNow.ToLocalTime();

will convert it to the local time from the Web Server's perspective.

If you have a WebServer in one location, serving clients in multiple countries, then you will need to perform this operation in javascript on the Client itself:

 myUTCDate.toLocaleTimeString();

http://www.java2s.com/Code/JavaScript/Date-Time/ConvertDatetoLocaleString.htm

like image 94
BonyT Avatar answered Oct 17 '22 11:10

BonyT


A DateTime value defines a particular date and time, it includes a Kind property that provides limited information about the time zone to which that date and time belongs.

The DateTimeOffset structure represents a date and time value, together with an offset that indicates how much that value differs from UTC. Thus, the value always unambiguously identifies a single point in time.

DateTimeOffset should be considered the default date and time type for application development as the uses for DateTimeOffset values are much more common than those for DateTime values.

See more info, code examples at: http://msdn.microsoft.com/en-us/library/bb384267.aspx

like image 23
Samuel Liew Avatar answered Oct 17 '22 13:10

Samuel Liew


DateTimeOffset represents the datetime as UTC datetime.

So

DateTimeOffset dtoNow = DateTimeOffset.Now;

is same as

DateTimeOffset dtoUTCNow = DateTimeOffset.UTCNow;

Here dtoNow will be equal to dtoUTCNow even though one was initialized to DateTimeOffset.Now and the other was initialize to DateTimeOffset.UTCNow;

So DatetimeOffset is good for storing the difference or Offset w.r.t UTC.

For more details refer to MSDN.

like image 3
Pradeep Avatar answered Oct 17 '22 12:10

Pradeep