`XmlConvert.ToDateTime(String)` 和 `XmlConvert.ToString(DateTime)` 输出不一致
我在使用 XmlConvert
和 DateTime
时遇到问题。
当使用 XmlConvert.ToDateTime(String)
转换某些字符串时,然后使用 XmlConvert.ToString(DateTime)
将结果 DateTime
转换回字符串,结果字符串为与原始字符串不同,并且取决于本地时区。
以下控制台应用程序代码演示了该问题:
using System;
using System.Xml;
namespace DateTimeXmlConvertTest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Original Value\t\t\t\tNew Value");
Console.WriteLine("--------------\t\t\t\t---------");
for (int i = -12; i <= 12; i++)
{
string sign = i < 0 ? "" : "+";
string originalString = "2011-10-01T01:18:54.6652000" + sign + i.ToString("D2") + ":00";
DateTime now = XmlConvert.ToDateTime(originalString);
string newValue = XmlConvert.ToString(now);
Console.WriteLine(originalString + "\t" + newValue);
}
}
}
}
在我的 PC(Windows 7 x64、时区 UTC+02:00 和夏令时)上运行此程序时,我有以下输出:
Original Value New Value
-------------- ---------
2011-10-01T01:18:54.6652000-12:00 2011-10-01T16:18:54.6652000+03:00
2011-10-01T01:18:54.6652000-11:00 2011-10-01T15:18:54.6652000+03:00
2011-10-01T01:18:54.6652000-10:00 2011-10-01T14:18:54.6652000+03:00
2011-10-01T01:18:54.6652000-09:00 2011-10-01T13:18:54.6652000+03:00
2011-10-01T01:18:54.6652000-08:00 2011-10-01T12:18:54.6652000+03:00
2011-10-01T01:18:54.6652000-07:00 2011-10-01T11:18:54.6652000+03:00
2011-10-01T01:18:54.6652000-06:00 2011-10-01T10:18:54.6652000+03:00
2011-10-01T01:18:54.6652000-05:00 2011-10-01T09:18:54.6652000+03:00
2011-10-01T01:18:54.6652000-04:00 2011-10-01T08:18:54.6652000+03:00
2011-10-01T01:18:54.6652000-03:00 2011-10-01T07:18:54.6652000+03:00
2011-10-01T01:18:54.6652000-02:00 2011-10-01T06:18:54.6652000+03:00
2011-10-01T01:18:54.6652000-01:00 2011-10-01T05:18:54.6652000+03:00
2011-10-01T01:18:54.6652000+00:00 2011-10-01T04:18:54.6652000+03:00
2011-10-01T01:18:54.6652000+01:00 2011-10-01T03:18:54.6652000+03:00
2011-10-01T01:18:54.6652000+02:00 2011-10-01T02:18:54.6652000+03:00
2011-10-01T01:18:54.6652000+03:00 2011-10-01T01:18:54.6652000+03:00
2011-10-01T01:18:54.6652000+04:00 2011-10-01T00:18:54.6652000+03:00
2011-10-01T01:18:54.6652000+05:00 2011-09-30T23:18:54.6652000+03:00
2011-10-01T01:18:54.6652000+06:00 2011-09-30T22:18:54.6652000+03:00
2011-10-01T01:18:54.6652000+07:00 2011-09-30T21:18:54.6652000+03:00
2011-10-01T01:18:54.6652000+08:00 2011-09-30T20:18:54.6652000+03:00
2011-10-01T01:18:54.6652000+09:00 2011-09-30T19:18:54.6652000+03:00
2011-10-01T01:18:54.6652000+10:00 2011-09-30T18:18:54.6652000+03:00
2011-10-01T01:18:54.6652000+11:00 2011-09-30T17:18:54.6652000+03:00
2011-10-01T01:18:54.6652000+12:00 2011-09-30T16:18:54.6652000+03:00
转换为然后再转换为不同结果时,如何可能细绳?
在这种情况下有没有办法让它输出原始字符串?
谢谢。
I having issues with XmlConvert
and DateTime
.
When converting some string with XmlConvert.ToDateTime(String)
then convert resulted DateTime
back to string with XmlConvert.ToString(DateTime)
and resulting string is different from the original string, and depending on local TimeZone.
The following console application code demonstrating the issue:
using System;
using System.Xml;
namespace DateTimeXmlConvertTest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Original Value\t\t\t\tNew Value");
Console.WriteLine("--------------\t\t\t\t---------");
for (int i = -12; i <= 12; i++)
{
string sign = i < 0 ? "" : "+";
string originalString = "2011-10-01T01:18:54.6652000" + sign + i.ToString("D2") + ":00";
DateTime now = XmlConvert.ToDateTime(originalString);
string newValue = XmlConvert.ToString(now);
Console.WriteLine(originalString + "\t" + newValue);
}
}
}
}
When running this program on my PC (Windows 7 x64, TimeZone UTC+02:00 with Daylight) i have the following output:
Original Value New Value
-------------- ---------
2011-10-01T01:18:54.6652000-12:00 2011-10-01T16:18:54.6652000+03:00
2011-10-01T01:18:54.6652000-11:00 2011-10-01T15:18:54.6652000+03:00
2011-10-01T01:18:54.6652000-10:00 2011-10-01T14:18:54.6652000+03:00
2011-10-01T01:18:54.6652000-09:00 2011-10-01T13:18:54.6652000+03:00
2011-10-01T01:18:54.6652000-08:00 2011-10-01T12:18:54.6652000+03:00
2011-10-01T01:18:54.6652000-07:00 2011-10-01T11:18:54.6652000+03:00
2011-10-01T01:18:54.6652000-06:00 2011-10-01T10:18:54.6652000+03:00
2011-10-01T01:18:54.6652000-05:00 2011-10-01T09:18:54.6652000+03:00
2011-10-01T01:18:54.6652000-04:00 2011-10-01T08:18:54.6652000+03:00
2011-10-01T01:18:54.6652000-03:00 2011-10-01T07:18:54.6652000+03:00
2011-10-01T01:18:54.6652000-02:00 2011-10-01T06:18:54.6652000+03:00
2011-10-01T01:18:54.6652000-01:00 2011-10-01T05:18:54.6652000+03:00
2011-10-01T01:18:54.6652000+00:00 2011-10-01T04:18:54.6652000+03:00
2011-10-01T01:18:54.6652000+01:00 2011-10-01T03:18:54.6652000+03:00
2011-10-01T01:18:54.6652000+02:00 2011-10-01T02:18:54.6652000+03:00
2011-10-01T01:18:54.6652000+03:00 2011-10-01T01:18:54.6652000+03:00
2011-10-01T01:18:54.6652000+04:00 2011-10-01T00:18:54.6652000+03:00
2011-10-01T01:18:54.6652000+05:00 2011-09-30T23:18:54.6652000+03:00
2011-10-01T01:18:54.6652000+06:00 2011-09-30T22:18:54.6652000+03:00
2011-10-01T01:18:54.6652000+07:00 2011-09-30T21:18:54.6652000+03:00
2011-10-01T01:18:54.6652000+08:00 2011-09-30T20:18:54.6652000+03:00
2011-10-01T01:18:54.6652000+09:00 2011-09-30T19:18:54.6652000+03:00
2011-10-01T01:18:54.6652000+10:00 2011-09-30T18:18:54.6652000+03:00
2011-10-01T01:18:54.6652000+11:00 2011-09-30T17:18:54.6652000+03:00
2011-10-01T01:18:54.6652000+12:00 2011-09-30T16:18:54.6652000+03:00
How is it possible when converting to and then from it results with different string?
Is there a way to make it output the original string in this case?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,
XmlConvert.ToDateTime(String)
过载已过时。该文档明确指定您应该使用XmlConvert.ToDateTime(String, XmlDateTimeSerializationMode)
相反。使用它,您可以指定XmlDateTimeSerializationMode
,记录如下:当您拥有
DateTime
的实例时,它的构造方式(例如,它的时区)将会丢失。时区将从原始字符串中正确解析并转换为 UTC 或本地时间,但转换的内容会在此过程中丢失。如果要保留时区偏移,请使用
DateTimeOffset
类。它的行为类似于
DateTime
,但保留了创建它的时区偏移量。XmlConvert
有一个ToDateTimeOffset
方法完全符合您的要求。这是一个有效的示例:您现在遇到的唯一问题是
+00:00
将被序列化为Z
,如果您格式化newString< 则可以避免这种情况。 /code> 你自己,就像这样:
First, the
XmlConvert.ToDateTime(String)
overload is obsolete. The documentation clearly specifies that you should useXmlConvert.ToDateTime(String, XmlDateTimeSerializationMode)
instead. With it, you get to specify theXmlDateTimeSerializationMode
, which is documented as follows:When you have an instance of
DateTime
, how it was constructed (its timezone, for instance) will be lost. The timezone will be properly parsed from the original string and converted to either UTC or local time, but what it was converted from is lost in the process.If you want to preserve the timezone offset, use the
DateTimeOffset
class. It behaves likeDateTime
, but preserves the timezone offset from which it was created.XmlConvert
has aToDateTimeOffset
method that does exactly what you want. Here's a working example:The only issue you have now is that
+00:00
will be serialized toZ
, which can be avoided if you format thenewString
yourself, like so:XmlConvert.ToDateTime(String, XmlDateTimeSerializationMode) 怎么样,
并保留时区偏移量,使用参数:
System.Xml.XmlDateTimeSerializationMode.RoundtripKind
What about XmlConvert.ToDateTime(String, XmlDateTimeSerializationMode),
and to preserve the timezone offset use the parametre :
System.Xml.XmlDateTimeSerializationMode.RoundtripKind