user3481630
user3481630

Reputation: 111

How to convert hours from h to hh in c#

I was able to convert the minutes to hours and minutes. e.g. 35 minutes is converted to 0:35 and 65 minutes to 1:5.

But I would like it to be displayed in the format of 00:35 and 01:05 respectively for previous two examples. Any suggestions here?

I tried the below code

        TimeSpan ts = TimeSpan.FromMinutes(intMins);           
        Console.WriteLine(string.Format("{0}:{1}", ts.Hours,  ts.Minutes ) );    

Upvotes: 1

Views: 429

Answers (1)

Curtis
Curtis

Reputation: 103348

Use string format 00:

Console.WriteLine(string.Format("{0:00}:{1:00}", ts.Hours,  ts.Minutes)); 

Or see the following SO post for formatting TimeSpan objects:

Convert TimeSpan from format "hh:mm:ss" to "hh:mm"

Upvotes: 7

Related Questions