Answer the question
In order to leave comments, you need to log in
How to make time format 99:59:59?
How to make the clock be up to 99? What would return 99:59:59 if you enter 359999 seconds? Also, is it possible to omit the year, month, day when creating DateTime if they are not needed (I did not find such overloads).
private string GetReadebleTime(int seconds)
{
var maxTime = 359999;
if (seconds > maxTime || seconds < 0)
return "0";
var hours = seconds / 3600;
seconds %= 3600;
var minutes = seconds / 60;
seconds %= 60;
DateTime dateTime = new DateTime(1, 1, 1, hours, minutes, seconds);
return dateTime.ToString("HH:mm:ss");
}
Answer the question
In order to leave comments, you need to log in
1. You need a TimeSpan, not a DateTime.
2. Out of the box, you can’t format the way you need, so you have to bike:
var max = 359999;
var ts = TimeSpan.FromSeconds(max);
var hours = (int)ts.TotalHours;
var minutes = ts.Minutes;
var seconds = ts.Seconds;
var formattedString = $"{hours}:{minutes}:{seconds}";
Console.WriteLine(formattedString); // 99:59:59
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question