How to parse string with hours greater than 24 to TimeSpan?

前端 未结 5 1639
一生所求
一生所求 2020-11-29 07:58

How to parse string like 30:15 to TimeSpan in C#? 30:15 means 30 hours and 15 minutes.

string span = \"30:15\";
TimeSpan ts = TimeSpan.FromHours(
    Convert         


        
5条回答
  •  心在旅途
    2020-11-29 08:33

    If you're certain that the format will always be "HH:mm" then try something like this:

    string span = "35:15";
    TimeSpan ts = new TimeSpan(int.Parse(span.Split(':')[0]),    // hours
                               int.Parse(span.Split(':')[1]),    // minutes
                               0);                               // seconds
    

提交回复
热议问题