`XmlConvert.ToDateTime(String)` 和 `XmlConvert.ToString(DateTime)` 输出不一致

发布于 2024-12-10 09:29:04 字数 3143 浏览 0 评论 0原文

我在使用 XmlConvertDateTime 时遇到问题。

当使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦幻之岛 2024-12-17 09:29:04

首先,XmlConvert.ToDateTime(String) 过载已过时。该文档明确指定您应该使用 XmlConvert.ToDateTime(String, XmlDateTimeSerializationMode) 相反。使用它,您可以指定 XmlDateTimeSerializationMode,记录如下:

XmlDateTimeSerializationMode 值之一,指定日期是否应转换为本地时间或保留为协调世界时 (UTC)(如果是 UTC 日期)。

当您拥有 DateTime 的实例时,它的构造方式(例如,它的时区)将会丢失。时区将从原始字符串中正确解析并转换为 UTC 或本地时间,但转换的内容会在此过程中丢失。

如果要保留时区偏移,请使用 DateTimeOffset类。它的行为类似于 DateTime,但保留了创建它的时区偏移量。 XmlConvert 有一个 ToDateTimeOffset 方法完全符合您的要求。这是一个有效的示例:

Console.WriteLine("{0,33}\t{1,33}", "Original Value", "New Value");
Console.WriteLine("{0}\t{0}", new String('-', 33));

for (int i = -12; i <= 12; i++)
{
    string sign = i < 0 ? "" : "+";
    string originalString = String.Format(
        "2011-10-01T01:18:54.6652123{0}{1:D2}:00", sign, i);
    DateTimeOffset dateTime = XmlConvert.ToDateTimeOffset(originalString);
    string newString = XmlConvert.ToString(dateTime);
    Console.WriteLine("{0}\t{1}", originalString, newString);
}

您现在遇到的唯一问题是 +00:00 将被序列化为 Z,如果您格式化 newString< 则可以避免这种情况。 /code> 你自己,就像这样:

string newString = String.Format("{0:yyyy-MM-ddTHH:mm:ss.fffffffzzz}", dateTime);

First, the XmlConvert.ToDateTime(String) overload is obsolete. The documentation clearly specifies that you should use XmlConvert.ToDateTime(String, XmlDateTimeSerializationMode) instead. With it, you get to specify the XmlDateTimeSerializationMode, which is documented as follows:

One of the XmlDateTimeSerializationMode values that specify whether the date should be converted to local time or preserved as Coordinated Universal Time (UTC), if it is a UTC date.

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 like DateTime, but preserves the timezone offset from which it was created. XmlConvert has a ToDateTimeOffset method that does exactly what you want. Here's a working example:

Console.WriteLine("{0,33}\t{1,33}", "Original Value", "New Value");
Console.WriteLine("{0}\t{0}", new String('-', 33));

for (int i = -12; i <= 12; i++)
{
    string sign = i < 0 ? "" : "+";
    string originalString = String.Format(
        "2011-10-01T01:18:54.6652123{0}{1:D2}:00", sign, i);
    DateTimeOffset dateTime = XmlConvert.ToDateTimeOffset(originalString);
    string newString = XmlConvert.ToString(dateTime);
    Console.WriteLine("{0}\t{1}", originalString, newString);
}

The only issue you have now is that +00:00 will be serialized to Z, which can be avoided if you format the newString yourself, like so:

string newString = String.Format("{0:yyyy-MM-ddTHH:mm:ss.fffffffzzz}", dateTime);
只想待在家 2024-12-17 09:29:04

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文