Possible Duplicate:
In C#, given a DateTime object, how do I get a ISO 8601 date in string format?
I've a variable with type DateTime in my C# code. How do I convert this to "Z" format?
Thanks.
Possible Duplicate:
In C#, given a DateTime object, how do I get a ISO 8601 date in string format?
I've a variable with type DateTime in my C# code. How do I convert this to "Z" format?
Thanks.
Do you mean converting to a string time/date like 2009-06-15 20:45:30Z
? If so then try:
mydate.ToUniversalTime().ToString("u");
See http://msdn.microsoft.com/en-us/library/az4se3k1.aspx for info on Standard Date and Time format strings. In particular read the entry on using the "Universal Sortable ("u") Format Specifier
" whcih explains why I have the ToUniversalTime
call in there.
If you mean the universal sortable date/time pattern:
string formatted = theDate.ToString("u");
Example result:
2009-06-15 20:45:30Z
Based on a quick search it seems to me that you simply mean UTC time, in which case you can use the ToUniversalTime
method of DateTime
, and when displaying as a string append "Z" to the end, or use the Universal Format Specifier when calling ToString
, or you could construct it using a number of format specifiers in the correct order.
I guess your format is ISO 8601: DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ")
Maybe a duplicate of this topic: Given a DateTime object, how do I get an ISO 8601 date in string format?