简体   繁体   English

DateTime.ParseExact的格式字符串 - 什么小时格式与“tt”一起使用?

[英]The format string for DateTime.ParseExact - what hour format works with “tt”?

I want to convert string 5/4/2017 2:15:50 PM to a datetime. 我想将字符串5/4/2017 2:15:50 PM转换为日期时间。 I used 我用了

statustime="5/4/2017 2:15:50 PM"
statustimefrm=DateTime.Parse(statustime, Globalization.CultureInfo.CurrentCulture)

and it worked, but I would rather use ParseExact. 它工作,但我宁愿使用ParseExact。 I used 我用了

statustimefrm=DateTime.Parse(statustime,  "M/d/yyyy HH:mm:ss tt",Globalization.CultureInfo.CurrentCulture)

but it gave me a format error. 但它给了我一个格式错误。 Would anybody know the format to use? 有人会知道要使用的格式吗?

"HH" is not compatible with "tt" - you either have 24 hour time or 12 hours plus AM/PM designators. “HH”与“tt”不兼容 - 您要么有24小时制,要么12小时加上AM / PM指示符。 You need to use h for time : 您需要使用h作为时间

 DateTime.ParseExact("5/4/2017 2:15:50 PM",  "M/d/yyyy hh:mm:ss tt",
       new CultureInfo("en-US")) 

这适合我

DateTime.ParseExact(timespan, "M/d/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM