How can I String.Format a TimeSpan object with a custom format in .NET?

后端 未结 19 1248
情深已故
情深已故 2020-11-22 12:25

What is the recommended way of formatting TimeSpan objects into a string with a custom format?

相关标签:
19条回答
  • 2020-11-22 12:59
    if (timeSpan.TotalDays < 1)
        return timeSpan.ToString(@"hh\:mm\:ss");
    
    return timeSpan.TotalDays < 2
        ? timeSpan.ToString(@"d\ \d\a\y\ hh\:mm\:ss")
        : timeSpan.ToString(@"d\ \d\a\y\s\ hh\:mm\:ss");
    

    All literal characters must be escaped.

    0 讨论(0)
提交回复
热议问题