简体   繁体   中英

How do I convert int values to hh:mm in C#?

I want to convert the int value 600 to 06:00 and 1700 to 17:00 in C#.

This is what I have tried so far:

int val = 600;
TimeSpan result = TimeSpan.FromHours(val);
string fromTimeString = result.ToString("hh':'mm");

How can this be achieved?

If you have only hours and minutes you can create the TimeSpan like this:

// Assuming val is always valid:
TimeSpan result = new TimeSpan(val / 100, val % 100, 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM