TimeSpan.Parse time format hhmmss

后端 未结 6 1510
忘掉有多难
忘掉有多难 2020-12-15 20:52

in c# i have time in format hhmmss like 124510 for 12:45:10 and i need to know the the TotalSeconds. i used the TimeSpan.Parse(\"12:45:10\").ToTalSeconds but it does\'nt tak

6条回答
  •  情深已故
    2020-12-15 21:15

    In case you want to work with also milliseconds like this format "01:02:10.055" then you may do as following;

    public static double ParseTheTime(string givenTime)
    {
    var time = DateTime.ParseExact(givenTime, "hh:mm:ss.fff", CultureInfo.InvariantCulture);
    return time.TimeOfDay.TotalSeconds;
    }
    

    This code will give you corresponding seconds. Note that you may increase the number of 'f's if you want to adjust precision points.

提交回复
热议问题