gpt4 book ai didi

c# - 如何解决 DateTimeInvalidLocalFormat 错误 : "A UTC DateTime is being converted to text in a format that is only correct for local times."?

转载 作者:行者123 更新时间:2023-11-30 18:22:51 28 4
gpt4 key购买 nike

尽管执行了 ToString(),但我在调试时遇到此错误:

A UTC DateTime is being converted to text in a format that is only correct for local times. This can happen when calling DateTime.ToString using the 'z' format specifier, which will include a local time zone offset in the output. In that case, either use the 'Z' format specifier, which designates a UTC time, or use the 'o' format string, which is the recommended way to persist a DateTime in text. This can also occur when passing a DateTime to be serialized by XmlConvert or DataSet. If using XmlConvert.ToString, pass in XmlDateTimeSerializationMode.RoundtripKind to serialize correctly. If using DataSet, set the DateTimeMode on the DataColumn object to DataSetDateTime.Utc.

public static string ToInterfaceString(this DateTime value)
{
return value != DateTime.MinValue ? value.ToString("yyyy-MM-ddTHH:mm:sszzz") : string.Empty;
}

在我刚刚开始开发的应用程序中,很多地方都使用了这种格式。那我实际上应该怎么办呢?将 zzz 替换为 Z?

更新 1:传递到我的扩展程序的 DateTime 被初始化为:

DateTimeCreated = DateTime.UtcNow;

奇怪的是,如果我将一些其他 DateTime 对象传递给此扩展程序,我不会收到任何错误/警告。

最佳答案

这只是一个绿色警告

因此 - 因为您似乎知道自己在做什么 - 您可以理解消息(这是正确的)并标记复选框以以后忽略此警告

关于c# - 如何解决 DateTimeInvalidLocalFormat 错误 : "A UTC DateTime is being converted to text in a format that is only correct for local times."?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33346052/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com