Question

DateTime.Now.ToUniversalTime() gives me "2013/1/13 4:31:39", but I want "2012-01-13T04:31:39Z", which is very common in website's rss xml node. I am going to use this format in html5 <time> attribute.

I know it can be done with .ToString("yyyy-MM-ddTHH:mm:ssZ") , but I am thinking if there's any existing API in .NET that can do this?

Was it helpful?

Solution

This does most of it except the 'T', it puts a space instead

dateValue.ToString("u")   //=> 2013-01-13 04:43:05Z

Also check out the round trip version i.e. passing "o" to ToString, only issue is it gives more precision that you seem to need

dateValue.ToString("o")       // => 2013-01-13T04:47:34.7559072Z
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top