Getting 'DateTime.ToString ()' to output the same string as XML serialization 'DateTime'

Is there a standard format DateTimefor use in C # that can be used with a method ToStringthat will create the same format that is created when serialized DateTimein XML?

For instance: 2013-03-20T13:32:45.5316112Z

+5
source share
3 answers

.ToString ("o") seemed to do the trick

+2
source

I think you should be specific:

dateTime.ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ");

You must be careful using the correct time zone. See here for more details .

+4
source

:

http://msdn.microsoft.com/en-us/library/az4se3k1.aspx#UniversalFull

, :

myDate.ToString("u");

:

DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
Console.WriteLine(date1.ToUniversalTime().ToString("u"));
// Displays 2008-04-10 13:30:00Z           

However, this is not quite what you need (although it probably will still work), so you may need to use a custom format:

DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
Console.WriteLine(date1.ToUniversalTime().ToString("yyyy-MM-dd'T'HH:mm:ss.fffffffZ"));
// Displays 2008-04-10T13:30:00.000000Z           
+2
source

All Articles