How do I format a timespan to show me total hours?

前端 未结 2 1182
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-10 03:05

I want to save the user\'s hours worked in a database varchar column, but by default, the formatted value includes days if the number of hours is more than 24.

相关标签:
2条回答
  • 2020-12-10 03:30

    Try TimeSpan.TotalHours

    String timeStamp = "40:00:00";
    var segments = timeStamp.Split(':');
    
    TimeSpan t = new TimeSpan(0, Convert.ToInt32(segments[0]), 
                   Convert.ToInt32(segments[1]), Convert.ToInt32(segments[2]));
    string time = string.Format("{0}:{1}:{2}", 
               ((int) t.TotalHours), t.Minutes, t.Seconds);
    
    0 讨论(0)
  • 2020-12-10 03:48

    You could do something like:

    TimeSpan time = ...;
    string timeForDisplay = (int)time.TotalHours + time.ToString(@"\:mm\:ss");
    
    0 讨论(0)
提交回复
热议问题