![](/img/trans.png)
[英]String to date parsing error “String was not recognized as a valid DateTime.”
[英]String date was not recognized as a valid DateTime.'
我正在使用字符串日期,例如
string d = "07/18/2022 08:33:15 PM"; // string datetime value
string w = "56.98"; // millisec
string c = d + ":" + w; // 07/18/2022 08:33:15 PM:56.98
int p = c.LastIndexOf(':');
我收到以下錯誤:
字符串“07/18/2022 08:33:15 PM”未被識別為有效的日期時間。
我使用的字符串日期如下:
DateTime dt = DateTime
.ParseExact(c.Substring(0, p), "d-M-yyyy H:m", CultureInfo.InvariantCulture)
.AddMilliseconds(double.Parse(c.Substring(p + 1), CultureInfo.InvariantCulture)); // here i am getting above mentioned error.
字符串 d 包含“PM”。
這里有什么問題?
請參閱下面顯示的屏幕截圖。
無需使用DateTime.ParseExact
,只需使用Parse
:
DateTime dt = DateTime.Parse(d, CultureInfo.InvariantCulture);
另外我不明白為什么您將毫秒任務過於復雜,只需首先將其聲明為double
精度,無需創建稍后必須解析的字符串:
double millis = 56.98;
dt = dt.AddMilliseconds(millis);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.