Why does TimeSpan.ParseExact not work

前端 未结 5 1031
既然无缘
既然无缘 2020-11-29 10:00

This is a bit wierd. Parsing a text field with a valid timespan fails if I try to be precise!

const string tmp = \"17:23:24\";
//works
var t1 = TimeSpan.Pars         


        
5条回答
  •  眼角桃花
    2020-11-29 10:29

    If you don't want to deal with the difference in format specifiers between TimeSpan.ParseExact and DateTime.ParseExact you can just parse your string as a DateTime and get the TimeOfDay component as a TimeSpan like this:

    var t2 = DateTime.ParseExact(tmp, "hh:mm:ss", CultureInfo.InvariantCulture).TimeOfDay;
    

提交回复
热议问题