Formatting C # DateTimeOffset in a specific format

Is there any link where I can find out how I can create a format for DateTimeOffset that will allow me to create such a string?

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 create the above string representation?

+5
source share
2 answers

The standard format specifier "O" or "o" corresponds to the custom format string yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffK for DateTime values ​​and the string yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffzzz custom format 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 

Link: https://msdn.microsoft.com/en-us/library/az4se3k1%28v=vs.110%29.aspx#Roundtrip

+5
source

What you want is a standardized ISO 8601 date / time combination.

The format string "o" provides you with the following:

 DateTimeOffset dto = new DateTimeOffset(DateTime.Now); string iso8601date = dto.ToString("o") 
+2
source

Source: https://habr.com/ru/post/1212793/


All Articles