获取“DateTime. tostring()”以输出与“DateTime”的XML序列化相同的字符串

时间:2022-01-07 02:26:15

Is there a standard DateTime format for use in C# that can be used with the ToString method that will produce the same format that is produced when you serialize a DateTime to XML?

在c#中是否有一个标准的DateTime格式,可以使用ToString方法使用,它将产生与序列化DateTime到XML时生成的相同格式?

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

例如:2013 - 03 - 20 - t13:32:45.5316112z

3 个解决方案

#1


4  

I think you have to be specific:

我认为你必须具体:

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

You have to be careful about using the right time zone. See here for more details.

你必须小心使用正确的时区。详情请参见这里。

#2


2  

Look here:

看这里:

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

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

The format you want is:

您需要的格式是:

myDate.ToString("u");

Example:

例子:

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're after (though probably would still work), therefore you may have 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           

#3


2  

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

.ToString(“o”)似乎起了作用。

#1


4  

I think you have to be specific:

我认为你必须具体:

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

You have to be careful about using the right time zone. See here for more details.

你必须小心使用正确的时区。详情请参见这里。

#2


2  

Look here:

看这里:

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

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

The format you want is:

您需要的格式是:

myDate.ToString("u");

Example:

例子:

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're after (though probably would still work), therefore you may have 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           

#3


2  

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

.ToString(“o”)似乎起了作用。