我的字符串包含h:mm:ss tt
格式时间,我想将此字符串转换为时间跨度
我在下面试过
string time = "5:49:41 PM";
TimeSpan Reportingtime = TimeSpan.Parse(time);
但是String was not recognized as a valid TimeSpan.
请帮帮我
答案 0 :(得分:2)
尝试在this发布
中删除它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;
答案 1 :(得分:1)
使用这种通用时间格式,它可以简化为:
string time = "5:19:41 PM";
TimeSpan reportingTime = DateTime.Parse(time).TimeOfDay;