2017-05-05 32 views
2

我想将字符串5/4/2017 2:15:50 PM转换为日期时间。我用DateTime.ParseExact的格式字符串 - 什么小时格式适用于“tt”?

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

它的工作,但我宁愿使用ParseExact。我用

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

但它给了我一个格式错误。有人会知道使用的格式吗?

回答

2

这对我的作品

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

把它改为DateTime.ParseExact(时间跨度, “M/d/yyyy的H:M:S TT”,CultureInfo.InvariantCulture) ; – Aparna

5

“HH”与“tt”不兼容 - 您可能有24小时或12小时以及AM/PM指示符。您需要使用h for time

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