MakeMeFly
MakeMeFly

Reputation: 43

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?

Upvotes: 2

Views: 1402

Answers (1)

György Kőszeg
György Kőszeg

Reputation: 18013

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);

Upvotes: 5

Related Questions