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

标签 c# datetime

尽管执行了 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/

相关文章:

c# - Azure Service Fabric 完成后如何重新运行 RunAsync 方法?

mysql - Codeigniter 日期比较 - 大于或小于日期时间字段不起作用

python - 如何查找每小时的占用情况?

c# - 表单之间的交互-如何从另一个表单更改表单的控件?

c# - 带有 CollectionViewSource 的 WPF ListView 未在 ViewModel 中获取 SelectedItem

c# - 不能分割线

c# - 基本表单控件在子表单中不可见

php - CodeIgniter/JQuery/MySQL 日期时间

c# - 将对象列表转换为日期时间列表,但我只想要日期而不是时间

c# - 如何防止超出范围的日期时间值错误?