简体   繁体   English

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

[英]Error, String not recognized as valid DateTime when trying to ParseExact time string

The following fails after executing the highlighted line. 执行突出显示的行后,以下操作失败。

在此输入图像描述

String was not recognized as a valid DateTime. 字符串未被识别为有效的DateTime。

It's happening all the sudden, worked when it was 12PM or so... ? 它突然发生,在12点左右工作......? Now its 4:54PM and no go. 现在是下午4:54而且没有去。 What the heck is going on? 到底他妈发生了什么?

You should be using hh:mm:ss tt as the format string - HH is for the 24-hour clock, at which point you're saying it's 4AM... but with PM as the AM/PM signifier. 您应该使用hh:mm:ss tt作为格式字符串HH用于24小时制,此时您说它是4AM ...但是PM作为AM / PM指示符。

Basically, use hh with tt , or HH on its own. 基本上,使用hhttHH本身。

Using Noda Time , you'd use: 使用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);
}

Note that it might be cleaner to return the LocalDateTime - do as much of your work as possible in the "natural" representation, only using strings when you really have to. 请注意,返回LocalDateTime可能更干净 - 在“自然”表示中尽可能多地完成工作,只在必要时使用字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将字符串识别为有效的DateTime ParseExact错误 - String was not recognized as a valid DateTime ParseExact Error 字符串未被识别为有效的DateTime ParseExact - String was not recognized as a valid DateTime ParseExact DateTime.ParseExact给出错误:字符串未被识别为有效的DateTime - DateTime.ParseExact give the error: String was not recognized as a valid DateTime DateTime.ParseExact引发以下错误“字符串未被识别为有效的DateTime” - DateTime.ParseExact is raising the following error “String was not recognized as a valid DateTime” Datetime.ParseExact“字符串未被识别为有效的日期时间”错误 - Datetime.ParseExact “String was not recognized as a valid DateTime” error 使用DateTime.ParseExact时,字符串未被识别为有效的DateTime - String was not recognized as a valid DateTime when using DateTime.ParseExact 在DateTime.ParseExact上未将字符串识别为有效的DateTime - String was not recognized as a valid DateTime on DateTime.ParseExact DateTime.ParseExact字符串未被识别为有效的日期时间 - DateTime.ParseExact String not recognized as valid datetime DateTime.ParseExact:字符串未被识别为有效的DateTime - DateTime.ParseExact: String was not recognized as a valid DateTime DateTime.ParseExact()-无法将字符串识别为有效的DateTime - DateTime.ParseExact() - String was not recognized as a valid DateTime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM