如何解决DateTimeInvalidLocalFormat错误:"UTC DateTime正在以仅适用于本地时间的格式转换为文本."? [英] How to solve DateTimeInvalidLocalFormat error: "A UTC DateTime is being converted to text in a format that is only correct for local times."?

查看:66
本文介绍了如何解决DateTimeInvalidLocalFormat错误:"UTC DateTime正在以仅适用于本地时间的格式转换为文本."?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然在ToDebug()中执行了调试,但我却收到此错误:

I am getting this error while in Debug though the ToString() is executed:

UTC日期时间正在以仅以下格式转换为文本在当地时间正确.呼叫时可能会发生这种情况DateTime.ToString使用"z"格式说明符,其中将包含一个输出中的本地时区偏移量.在这种情况下,请使用"Z"格式说明符,用于指定UTC时间,或使用"o"格式字符串,这是将DateTime保留在文本中的推荐方法.当传递DateTime进行序列化时,也会发生这种情况XmlConvert或数据集.如果使用XmlConvert.ToString,请传入XmlDateTimeSerializationMode.RoundtripKind可以正确序列化.如果使用DataSet,将DataColumn对象上的DateTimeMode设置为DataSetDateTime.Utc.

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?

In the app that I've just starting to work on it is used this format in many places. So what should I do in fact? Replace zzz with Z?

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

Update 1: the DateTime that is passed to my extension is initiated to:

DateTimeCreated = DateTime.UtcNow;

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

Weird thing is that if I pass to this extension some other DateTime objects I don't receive any error/warning.

推荐答案

仅是绿色警告.

所以-您似乎知道自己在做什么-您可以理解该消息(正确),并选中复选框以将来忽略此警告.

So - as you seem to know what you are doing - you can just comprehend the message (which is correct) and mark the checkbox to Ignore this warning in the future.

这篇关于如何解决DateTimeInvalidLocalFormat错误:"UTC DateTime正在以仅适用于本地时间的格式转换为文本."?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆