Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I format DateTime to web UTC format?

I have a DateTime which 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?

like image 359
Grzenio Avatar asked Nov 30 '09 16:11

Grzenio


People also ask

How do I display DateTime in UTC?

A time zone offset of "+hh:mm" indicates that the date/time uses a local time zone which is "hh" hours and "mm" minutes ahead of UTC. A time zone offset of "-hh:mm" indicates that the date/time uses a local time zone which is "hh" hours and "mm" minutes behind UTC.

How do you convert date to UTC format?

To convert a JavaScript date object to a UTC string, you can use the toUTCString() method of the Date object. The toUTCString() method converts a date to a string, using the universal time zone. Alternatively, you could also use the Date. UTC() method to create a new Date object directly in UTC time zone.

How do I convert DateTime to timezone?

DateTime currentTime = TimeZoneInfo. ConvertTime(DateTime. Now, TimeZoneInfo. FindSystemTimeZoneById("Central Standard Time"));


1 Answers

string foo = yourDateTime.ToUniversalTime()                          .ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'"); 
like image 184
LukeH Avatar answered Oct 02 '22 15:10

LukeH