[英]Cannot convert dateTime string with Datetime.ParseExact
This is the exact string i have: 18-2-2014 00:00:00 这是我的确切字符串:18-2-2014 00:00:00
My code bumps into this error: 我的代码碰到了这个错误:
An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll mscorlib.dll中发生了未处理的“System.FormatException”类型异常
Additional information: String was not recognized as a valid DateTime. 附加信息:字符串未被识别为有效的DateTime。
At this line: 在这一行:
newDateTime = DateTime.ParseExact(
part[0],
"dd-mm-jjjj hh:mm:ss",
CultureInfo.InvariantCulture
); //I tried a couple of time string variations, but it should be the above or "dd-m-jjjj hh:mm:ss".
What am i doing wrong? 我究竟做错了什么?
mm
specifier is for minutes. mm
说明符是几分钟。 Use M
specifier which is for months ( 1
to 12
). 使用M
说明符 ,持续数月( 1
到12
)。
hh
format is for 01
to 12
( 12-hour clock ). hh
格式为01
到12
( 12小时制 )。 It doesn't have 00
as an hour. 它没有00
小时。 That's why you should use HH
format which is for 00
to 23
( 24-hour clock ). 这就是你应该使用00
到23
( 24小时制 )的HH
格式的原因。
And there is no jjjj
date and time format specifier which I think you want to use yyyy
format. 并且没有jjjj
日期和时间格式说明符,我认为您想要使用yyyy
格式。
var s = "18-2-2014 00:00:00";
var date = DateTime.ParseExact(s,
"dd-M-yyyy HH:mm:ss",
CultureInfo.InvariantCulture);
Console.WriteLine(date);
Output will be; 输出将是;
2/18/2014 12:00:00 AM
Here a demonstration
. 这是一个demonstration
。
For more information, take a look at; 有关更多信息,请查看;
NOTE : As Jeppe Stig Nielsen pointed , since we don't know your culture exactly (can be nl-BE
or nl-NL
) your ShortDatePattern
day for is d
not dd
. 注 :由于叶普斯蒂格尼尔森指出 ,因为我们不知道你的文化究竟(可以是nl-BE
或nl-NL
)的ShortDatePattern
一天是d
不dd
。
That's why you might need to use d
format instead dd
. 这就是为什么你可能需要使用d
格式代替dd
。
Try This: 试试这个:
newDateTime = DateTime.ParseExact(part[0],"dd-M-yyyy HH:mm:ss",
CultureInfo.InvariantCulture);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.