gpt4 book ai didi

c# - DateTime.ParseExact() 失败

转载 作者:行者123 更新时间:2023-12-02 20:21:15 26 4
gpt4 key购买 nike

我正在尝试使用 ParseExact() 方法解析字符串:

DateTime.ParseExact(@"UTC 2014-07-08 13:11:40.396", @"UTC yyyy-MM-dd HH:mm:ss.FFF",null);

这会给出以下错误消息:

DateTime.ParseExact(@"UTC 2014-07-08 13:11:40.396", @"UTC yyyy-MM-dd HH:mm:ss.FFF",null) threw an exception of type 'System.FormatException' base: {"String was not recognized as a valid DateTime."}

最佳答案

尝试CultureInfo.InvariantCulture。这将迫使它忽略当前的区域性设置(通常我必须在生产中使用它来避免这些问题)

DateTime.ParseExact(@"UTC 2014-07-08 13:11:40.396", @"UTC yyyy-MM-dd HH:mm:ss.FFF",CultureInfo.InvariantCulture);

来自http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.invariantculture(v=vs.110).aspx

Unlike culture-sensitive data, which is subject to change by user customization or by updates to the .NET Framework or the operating system, invariant culture data is stable over time and across installed cultures and cannot be customized by users. This makes the invariant culture particularly useful for operations that require culture-independent results, such as formatting and parsing operations that persist formatted data, or sorting and ordering operations that require that data be displayed in a fixed order regardless of culture.

关于c# - DateTime.ParseExact() 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24633423/

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