Sunday, April 24, 2016

C# DateTime vs DateTimeOffset

DateTimeOffset is similar to DateTime except it allows explicit specification of "offset" of the date time from UTC.

CODE

DateTime dateTime = new DateTime(2016, 4, 22, 0, 0, 0, DateTimeKind.Unspecified);            Debug.WriteLine(DateTimeKind.Unspecified.ToString() + ": " + dateTime.ToString("o"));
            // json serialize applies ToString("o")
            Debug.WriteLine("Json Serialize: " + Newtonsoft.Json.JsonConvert.SerializeObject(dateTime));

            DateTime dateTimeUTC = new DateTime(2016, 4, 22, 0, 0, 0, DateTimeKind.Utc);
            Debug.WriteLine(DateTimeKind.Utc.ToString() + ": " + dateTimeUTC.ToString("o"));
            Debug.WriteLine("Json Serialize: " + Newtonsoft.Json.JsonConvert.SerializeObject(dateTimeUTC));

            DateTime dateTimeLocal = new DateTime(2016, 4, 22, 0, 0, 0, DateTimeKind.Local);
            Debug.WriteLine(DateTimeKind.Local.ToString() + ": " + dateTimeLocal.ToString("o"));
            Debug.WriteLine("Json Serialize: " + Newtonsoft.Json.JsonConvert.SerializeObject(dateTimeLocal));

            DateTime dateTimeLocalNow = DateTime.Now;
            Debug.WriteLine(dateTimeLocalNow.Kind.ToString() + ": " + dateTimeLocalNow.ToString("o"));
            Debug.WriteLine("Json Serialize: " + Newtonsoft.Json.JsonConvert.SerializeObject(dateTimeLocalNow));

            DateTimeOffset dateTimeOffsetNoOffset = new DateTimeOffset(2016, 4, 22, 0, 0, 0, new TimeSpan());
            Debug.WriteLine("No offset: " + dateTimeOffsetNoOffset.ToString("o"));
            Debug.WriteLine("Json Serialize: " + Newtonsoft.Json.JsonConvert.SerializeObject(dateTimeOffsetNoOffset));

            DateTimeOffset dateTimeOffsetUTC = new DateTimeOffset(2016, 4, 22, 0, 0, 0, new TimeSpan(0, 0, 0));
            Debug.WriteLine("Zero offset: " + dateTimeOffsetUTC.ToString("o"));
            Debug.WriteLine("Json Serialize: " + Newtonsoft.Json.JsonConvert.SerializeObject(dateTimeOffsetUTC));

            DateTimeOffset dateTimeOffsetSpecified = new DateTimeOffset(2016, 4, 22, 0, 0, 0, new TimeSpan(-7, 0, 0));
            Debug.WriteLine("Specified offset: " + dateTimeOffsetSpecified.ToString("o"));
            Debug.WriteLine("Json Serialize: " + Newtonsoft.Json.JsonConvert.SerializeObject(dateTimeOffsetSpecified));

            DateTimeOffset dateTimeOffsetLocalNow = DateTimeOffset.Now;
            Debug.WriteLine("Local offset: " + dateTimeOffsetLocalNow.ToString("o"));
            Debug.WriteLine("Json Serialize: " + Newtonsoft.Json.JsonConvert.SerializeObject(dateTimeOffsetLocalNow));

            DateTimeOffset dateTimeOffsetUtcNow = DateTimeOffset.UtcNow;
            Debug.WriteLine("Utc offset: " + dateTimeOffsetUtcNow.ToString("o"));
            // json serialize applies ToString("o")
            Debug.WriteLine("Json Serialize: " + Newtonsoft.Json.JsonConvert.SerializeObject(dateTimeOffsetUtcNow));

OUTPUT

Unspecified: 2016-04-22T00:00:00.0000000
Json Serialize: "2016-04-22T00:00:00"

Utc: 2016-04-22T00:00:00.0000000Z
Json Serialize: "2016-04-22T00:00:00Z"

Local: 2016-04-22T00:00:00.0000000-07:00
Json Serialize: "2016-04-22T00:00:00-07:00"

Local: 2016-04-24T12:18:09.1892907-07:00
Json Serialize: "2016-04-24T12:18:09.1892907-07:00"

No offset: 2016-04-22T00:00:00.0000000+00:00
Json Serialize: "2016-04-22T00:00:00+00:00"

Zero offset: 2016-04-22T00:00:00.0000000+00:00
Json Serialize: "2016-04-22T00:00:00+00:00"

Specified offset: 2016-04-22T00:00:00.0000000-07:00
Json Serialize: "2016-04-22T00:00:00-07:00"

Local offset: 2016-04-24T12:18:09.1892907-07:00
Json Serialize: "2016-04-24T12:18:09.1892907-07:00"

Utc offset: 2016-04-24T19:18:09.1892907+00:00
Json Serialize: "2016-04-24T19:18:09.1892907+00:00"