How convert TimeSpan to 24 hours and minutes String?

前端 未结 5 925
生来不讨喜
生来不讨喜 2020-12-29 04:09

I use this code for converting Timespan to String (for ex: 14:53) :

myTimeSpan.ToString(\"hh:mm\");

but this erro

5条回答
  •  半阙折子戏
    2020-12-29 05:09

    From TimeSpan.ToString Method (String)

    TimeSpan t = new TimeSpan(14, 53, 0);
    Console.WriteLine(t.ToString(@"hh\:mm"));
    

    As an alternative you can use String.Format like;

    Console.WriteLine(String.Format("{0}:{1}", t.Hours, t.Minutes));
    
    • Standard TimeSpan Format Strings
    • Custom TimeSpan Format Strings

    Remember, TimeSpan.ToString(String) overload only avaiable for .NET 4 or higher.

提交回复
热议问题