XML 的日期已保存为一天延迟

Date from XML saved as one day delay

我正在读取来自 XML 的日期字符串,示例如下:

<fundingStart value="20/04/2013"/>

我可以从 XML 中正确检索它,并且我将其转换为 DateTime,然后在 CouchBase 中保存为 JSON 格式。

但是,我的问题是保存数据时,延迟了一天。在上面 XML 的日期中,它被保存为 19/04/2013。

我的代码:

    if(!String.IsNullOrEmpty(source.FundingStartDate.Value))
    {
        DateTime startDate = DateTime.ParseExact(source.FundingStartDate.Value.ToString(), "d/M/yyyy", System.Globalization.CultureInfo.InvariantCulture);
        destination.Funding.Start = startDate;
    }

时区。

20/04/2013 被解析为 2013-04-20 00:00 当地时间。 CouchBase 与许多其他(文档)数据库一样,在内部将日期保存为 UTC。

因此,根据您所在的时区,将本地时间转换为 UTC 将产生 2013-04-19 23:00,GMT+1。

快速解决方法是在读取日期时调用 DateTime.ToLocalTime(),因此:

 destination.Funding.Start = destination.Funding.Start.ToLocalTime();

我确定 CouchBase C# SDK 有一些选项可以让从数据库读取的每个日期时间都发生这种情况。