Reputation: 15435
Is there any reference where I could find out how I can create a format for the DateTimeOffset that would enable me to produce a string like this?
2016-10-01T06:00:00.000000+02:00
I have a DateTimeOffset that I can work with, but I'm not sure how I could format it to produce the above string representation?
Upvotes: 13
Views: 37468
Reputation: 174700
What you want is an ISO 8601-standardized combined Date and Time string.
The "o" format string provides you with just that:
DateTimeOffset dto = new DateTimeOffset(DateTime.Now);
string iso8601date = dto.ToString("o")
Upvotes: 12
Reputation: 16983
The "O" or "o" standard format specifier corresponds to the
yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffK
custom format string for DateTime values and to theyyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffzzz
custom format string for DateTimeOffset values.
DateTime lDat = new DateTime(2009, 6, 15, 13, 45, 30, DateTimeKind.Local);
Console.WriteLine("{0} ({1}) --> {0:O}\n", lDat, lDat.Kind);
// 6/15/2009 1:45:30 PM (Local) --> 2009-06-15T13:45:30.0000000-07:00
DateTimeOffset dto = new DateTimeOffset(lDat);
Console.WriteLine("{0} --> {0:O}", dto);
// 6/15/2009 1:45:30 PM -07:00 --> 2009-06-15T13:45:30.0000000-07:00
Reference: https://msdn.microsoft.com/en-us/library/az4se3k1%28v=vs.110%29.aspx#Roundtrip
Upvotes: 13