[英]Error, String not recognized as valid DateTime when trying to ParseExact time string
執行突出顯示的行后,以下操作失敗。
字符串未被識別為有效的DateTime。
它突然發生,在12點左右工作......? 現在是下午4:54而且沒有去。 到底他媽發生了什么?
您應該使用hh:mm:ss tt
作為格式字符串HH
用於24小時制,此時您說它是4AM ...但是PM作為AM / PM指示符。
基本上,使用hh
與tt
或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
可能更干凈 - 在“自然”表示中盡可能多地完成工作,只在必要時使用字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.