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

how to parse time in timespan which is in hh:mm:ss tt format in c#

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

下面我试过了

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

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

请帮帮我

按照thisPost

中描述的方式尝试
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;