Convert Time 24 hours to 00 hours

1

I have an account in int, that 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 00:00? Because of 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 System.Globalization.GregorianCalendar.

I made an if, like this:

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

But do not you have some direct conversion?

    
asked by anonymous 06.06.2018 / 15:22

1 answer

1

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

"HH" format appears in 24-hour format. Lowercase "hh" format appears in 12 hour AM / PM format.

Example:

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

Console.WriteLine(data.ToString("HH:mm:ss"));
    
29.06.2018 / 16:53