Convert hour 24 hours to 00 hours

I have an account in int, i need to return it for hours, and it is working perfectly, the error occurs when the time appears 24, and it should appear 00: 00. Here's how I'm doing:

string horaStg;
decimal valor = int.Parse(item.HoraInicio);
valor = (valor / 60);
var inicio = valor.ToString().Split(char.Parse(","));
string hora = inicio[0];
try
{
    string minuto = "0," + inicio[1];
    string m = Math.Round(decimal.Parse(minuto.ToString()) * 60).ToString();
    horaStg = DateTime.Parse(hora + ":" + m.Substring(0, 2)).ToString("HH:mm");
}
catch
{
    horaStg = DateTime.Parse(hora + ":" + "00").ToString("HH:mm");
}

How can I convert to appear at 00:00 ? For the way it is when it is midnight, it appears 24 and returns me the following error:

The DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar.

I made an if, like this:

if (hora == "24")
{
    hora = "00";
}

But don't you have any conversion that you do directly ?

 1
Author: rLinhares, 2018-06-06

1 answers

If you take your DateTime variable and ToString it by passing the hour format with the capital letter "H", it will already appear in the 00 hour format.

Format uppercase " HH " appears in 24-hour format. Format lowercase " hh " appears in the format 12 hours AM/PM.

Example:

DateTime data = new DateTime(2018, 06, 29, 00, 11, 49);

Console.WriteLine(data.ToString("HH:mm:ss"));
 1
Author: Pedro Paulo, 2018-06-29 14:53:44