为什么我可以将该代码应用于其他形式的 .cs
string dt = day + "/" + month + "/" + year;
DateTime newDate = DateTime.ParseExact(dt, "dd/MM/yyyy", null);
总是报错
String was not recognized as a valid DateTime.
必须这样改
"dd/MM/yyyy" --> "d/M/yyyy"
但在其他形式的 .cs 中,该代码有效。不需要更改该字符串
最佳答案
通过指定 dd
和 MM
,您需要输入 2 个字符宽。您的代码适用于 10/10/2015
,但不适用于 1/1/2015
。
更改您的代码以允许单个日期和月份字符,您会没事的:
DateTime newDate = DateTime.ParseExact(dt, "d/M/yyyy", null);
关于c# - DateTime.ParseExact() 令人困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32902600/