gpt4 book ai didi

c# - 错误,尝试 ParseExact 时间字符串时,字符串未被识别为有效的 DateTime

转载 作者:行者123 更新时间:2023-11-30 19:57:45 25 4
gpt4 key购买 nike

执行突出显示的行后,以下操作失败。

enter image description here

String was not recognized as a valid DateTime.

它突然发生,在中午 12 点左右开始工作......?现在是下午 4 点 54 分,不能去。这到底是怎么回事?

最佳答案

您应该使用 hh:mm:ss tt 作为格式字符串 - HH 用于 24 小时制,此时您说的是凌晨 4 点...但以 PM 作为 AM/PM 指示符。

基本上,hhtt 一起使用,或者 HH 单独使用。

使用 Noda Time ,你会使用:

private static readonly LocalTimePattern TimePattern = 
LocalTimePattern.CreateWithInvariantCulture("hh:mm:ss tt");
// TODO: Check this is what you want! We can't tell from your example.
private static readonly LocalDatePattern DatePattern =
LocalDatePattern.CreateWithInvariantCulture("dd/MM/yyyy");
private static readonly LocalDateTimePattern DateTimePattern =
LocalDatePattern.CreateWithInvariantCulture("yyyy-MM-dd HH:mm:ss");

public static string GetMergedDateTime(string dateText, string timeText)
{
// The Value property throws an exception if parsing failed. You can
// check that with the Success property first though.
LocalDate date = DatePattern.Parse(dateText).Value;
LocalTime time = TimePattern.Parse(timeText).Value;
LocalDateTime dateTime = date + time;
return DateTimePattern.Format(dateTime);
}

请注意,返回 LocalDateTime 可能更简洁 - 尽可能多地以“自然”表示形式完成您的工作,仅在真正需要时才使用字符串。

关于c# - 错误,尝试 ParseExact 时间字符串时,字符串未被识别为有效的 DateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28842782/

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