gpt4 book ai didi

C#,从字符串错误中解析日期

转载 作者:太空狗 更新时间:2023-10-30 00:05:28 26 4
gpt4 key购买 nike

在 C# 中,我从字符串中解析日期,但它给我错误

DateTime.Parse("07/26/2012");

错误

 System.FormatException: String was not recognized as a valid DateTime.
at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
at System.DateTime.Parse(String s)

与日期格式有关吗?这与我的电脑设置有关吗?

谢谢

最佳答案

默认情况下,Parse 使用您当前的文化。 ParseExact允许您手动指定日期格式。

试试这个:

DateTime date = DateTime.ParseExact("07/26/2012", "MM/dd/yyyy", CultureInfo.InvariantCulture);

InvariantCulture 选项允许您忽略系统上的当前文化设置。

关于C#,从字符串错误中解析日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11670930/

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