如何在c#中以hh:mm:ss tt格式解析时间跨度



我有一个包含h:mm:ss tt格式时间的字符串,我想将此字符串转换为时间跨度

我试过低于

 string time = "5:49:41 PM";
 TimeSpan Reportingtime = TimeSpan.Parse(time);

String was not recognized as a valid TimeSpan. 出现错误

请帮助我

试着像这个Post 中描述的那样

string s = "5:19:41 PM";
DateTime t = DateTime.ParseExact(s, "h:mm:ss tt", CultureInfo.InvariantCulture); 
//if you really need a TimeSpan this will get the time elapsed since midnight:
TimeSpan ts = t.TimeOfDay;

使用这种常见的时间格式,它可以简化为:

string time = "5:19:41 PM";
TimeSpan reportingTime = DateTime.Parse(time).TimeOfDay;

相关内容

最新更新