How can I format the DateTime format to UTC on the Internet?

I have a DateTime that I want to format to "2009-09-01T00: 00: 00.000Z", but the following code gives me "2009-09-01T00: 00: 00.000 + 01: 00" (both lines):

new DateTime(2009, 9, 1, 0, 0, 0, 0, DateTimeKind.Utc).ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffzzz") new DateTime(2009, 9, 1, 0, 0, 0, 0, DateTimeKind.Utc).ToUniversalTime().ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffzzz") 

Any ideas how to make it work?

+46
c # datetime utc
Nov 30 '09 at 16:40
source share
8 answers
 string foo = yourDateTime.ToUniversalTime() .ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'"); 
+96
Nov 30 '09 at 16:45
source share

Why not just use the round-trip format specifier ("o", "o") ?

The standard format specifier โ€œOโ€ or โ€œoโ€ is a custom date and time format string using a template that stores time zone information and emits a result string that complies with ISO 8601. For DateTime values, this format specifier is for storing date and time values along with the DateTime.Kind property in the text. You can parse a formatted string using the DateTime.Parse (String, IFormatProvider, DateTimeStyles) or DateTime.ParseExact method, if the styles parameter is set to DateTimeStyles.RoundtripKind.

The standard format specifier "O" or "o" corresponds to the user-format string "yyyy" - "MM" - "dd'T'HH": 'mm': 'ss..fffffffK for DateTime values โ€‹โ€‹and to the user-format string "yyyy" - "MM" - "dd'T'HH": 'mm': 'ss .. fffffffzzz "for DateTimeOffset values. This line contains pairs of single quotes that enclose individual characters, such as hyphens, colons and the letter" T ", indicate that a single character is a literal that cannot be changed. Apostrophes are not displayed on the output line.

The standard format specifier is O "or" o "(and the custom format string" yyyy '-' MM '-' dd'T'HH ':' mm ':' ss '.' FffffffK) takes advantage of the three ways that ISO 8601 represents time zone information to save the Kind property for DateTime values:

 public class Example { public static void Main() { DateTime dat = new DateTime(2009, 6, 15, 13, 45, 30, DateTimeKind.Unspecified); Console.WriteLine("{0} ({1}) --> {0:O}", dat, dat.Kind); DateTime uDat = new DateTime(2009, 6, 15, 13, 45, 30, DateTimeKind.Utc); Console.WriteLine("{0} ({1}) --> {0:O}", uDat, uDat.Kind); DateTime lDat = new DateTime(2009, 6, 15, 13, 45, 30, DateTimeKind.Local); Console.WriteLine("{0} ({1}) --> {0:O}\n", lDat, lDat.Kind); DateTimeOffset dto = new DateTimeOffset(lDat); Console.WriteLine("{0} --> {0:O}", dto); } } // The example displays the following output: // 6/15/2009 1:45:30 PM (Unspecified) --> 2009-06-15T13:45:30.0000000 // 6/15/2009 1:45:30 PM (Utc) --> 2009-06-15T13:45:30.0000000Z // 6/15/2009 1:45:30 PM (Local) --> 2009-06-15T13:45:30.0000000-07:00 // // 6/15/2009 1:45:30 PM -07:00 --> 2009-06-15T13:45:30.0000000-07:00 
+39
Sep 16 '14 at 17:26
source share

The best format to use is "yyyy" - "MM" - "dd'T'HH": 'mm': 'ss'. 'fffK ".

The last line of K will be changed to "Z" if the date is UTC or the time zone (+ -hh: mm) if it is local. ( http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx )

As LukeH said, it is useful to use ToUniversalTime if you want all dates to be UTC.

End Code:

 string foo = yourDateTime.ToUniversalTime() .ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffK"); 
+6
May 23 '13 at 4:12
source share

You want to use the DateTimeOffset class.

 var date = new DateTimeOffset(2009, 9, 1, 0, 0, 0, 0, new TimeSpan(0L)); var stringDate = date.ToString("u"); 

Sorry, I skipped your original formatting with miliseconds

 var stringDate = date.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'"); 
+3
Nov 30 '09 at 16:45
source share

Some people have pointed out that โ€œToUniversalTime is somewhat unsafe in that it can cause unintentional incorrect temporary mailings. Extending this, I provide a more detailed example of the solution. The sample here creates an extension for the DateTime object that safely returns a UTC DateTime where you can use ToString optional...

 class Program { static void Main(string[] args) { DateTime dUtc = new DateTime(2016, 6, 1, 3, 17, 0, 0, DateTimeKind.Utc); DateTime dUnspecified = new DateTime(2016, 6, 1, 3, 17, 0, 0, DateTimeKind.Unspecified); //Sample of an unintended mangle: //Prints "2016-06-01 10:17:00Z" Console.WriteLine(dUnspecified.ToUniversalTime().ToString("u")); //Prints "2016 - 06 - 01 03:17:00Z" Console.WriteLine(dUtc.SafeUniversal().ToString("u")); //Prints "2016 - 06 - 01 03:17:00Z" Console.WriteLine(dUnspecified.SafeUniversal().ToString("u")); } } public static class ConvertExtensions { public static DateTime SafeUniversal(this DateTime inTime) { return (DateTimeKind.Unspecified == inTime.Kind) ? new DateTime(inTime.Ticks, DateTimeKind.Utc) : inTime.ToUniversalTime(); } } 
+2
Dec 01 '16 at 19:07
source share
 string.Format("{0:yyyy-MM-ddTHH:mm:ss.FFFZ}", DateTime.UtcNow) 

returns 2017-02-10T08: 12: 39.483Z

+1
Feb 10 '17 at 8:13
source share

This code works for me:

 var datetime = new DateTime(2017, 10, 27, 14, 45, 53, 175, DateTimeKind.Local); var text = datetime.ToString("o"); Console.WriteLine(text); -- 2017-10-27T14:45:53.1750000+03:00 // datetime from string var newDate = DateTime.ParseExact(text, "o", null); 
0
Oct 27 '17 at 12:50
source share

Try the following:

 DateTime date = DateTime.ParseExact( "Tue, 1 Jan 2008 00:00:00 UTC", "ddd, d MMM yyyy HH:mm:ss UTC", CultureInfo.InvariantCulture); 

Previously Asked Question

-2
Nov 30 '09 at 16:41
source share



All Articles