gpt4 book ai didi

c# - DateTime.ParseExact 给出的字符串未被识别为有效的日期时间。

转载 作者:IT王子 更新时间:2023-10-29 04:52:26 24 4
gpt4 key购买 nike

我正在尝试将日期字符串解析为 DateTime 变量。我发现 ParseExact 是这样做的方法,但我尝试这个我得到了错误:

字符串未被识别为有效的日期时间。

string timeFormat = "dd-MM-yyyy hh:mm:ss";
DateTime startDate = DateTime.ParseExact(reader["startdate"].ToString(), timeFormat, CultureInfo.InvariantCulture);
DateTime nextDate = DateTime.ParseExact(reader["nextdate"].ToString(), timeFormat, null);

我已经尝试过使用 null(恰好在另一个页面上工作)和 CultureInfo.InvariantCulture

reader["startdate"].ToString() 输出:01-08-2012 15:39:09

reader["nextdate"].ToString() 输出:01-08-2012 15:39:09

我认为它应该起作用,但它不起作用。

有人知道哪里出了问题吗? :)

最佳答案

您在格式字符串中使用了 hh。这是一个 12 小时的“一天中的小时”字段。值 15 不在范围内...

您需要 HH,它是 24 小时制说明符。

参见 MSDN custom date and time format strings documentation获取更多信息。

关于c# - DateTime.ParseExact 给出的字符串未被识别为有效的日期时间。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11766022/

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