Convert time in HH format: MM to int

1

I'm at a crossroads it turns out that I'm making a .Net application that receives me an example value 00:34 and that value I want to convert to int to pass it to the database example my time I get is:

00:14 and I want to convert it to an integer in relation that 00:00 is the integer 1 (ignoring the rest) 01:13 is the integer 2 and so on always ignoring the minutes only taking into account the hours here my code

 movCentrales.Add(new tb_Movimientos_centrales
{

    Fecha = Convert.ToDateTime(dtfechaRio.Rows[0].ToString().Substring(6, 4) + "-" + dtfechaRio.Rows[0].ToString().Substring(3, 2) + "-" + dtfechaRio.Rows[0].ToString().Substring(0, 2)),
    Hora_movimiento = Convert.ToString(drhoraMov["Hora Movi#"]),                                    
    Horario = Convert.ToInt32(drHorario["Hora Movi#"]) +1,
    Central_equipo = Convert.ToString(drCentralEquipo["Central/Equipo"]),
    Consigna = Convert.ToString(drConsigna["Consigna"]),
    Central_Configura = Convert.ToString(drCentralConfigura["F1"]),


});
dbContRio.SaveChanges();

The time I want to convert is the variable Timetable I was thinking about doing a convert.toint32 and adding one to some idea of how to do it: more info:

Hora:
00:14 = 1 --
00:59 = 1 --
01:25 = 2 --
01:00 = 2 --
02:13 = 3 --

and so on.

    
asked by Joel Baez 24.04.2018 в 17:07
source

2 answers

1

You have to convert the value of drHorario["Hora Movi#"] to a date to be able to have the time this can be done by selecting a part of the string or by making Convert.ToDateTime() to the value drHorario["Hora Movi#"] and using the property Hour .

//Ejemplo 1
Horario = Convert.ToInt32(drHorario["Hora Movi#"].ToString().Substring(0, 2)) +1

//Ejemplo 2
Horario = Convert.ToDateTime(drHorario["Hora Movi#"].ToString()).Hour + 1

In both examples you must validate that it is a correct format of hours and minutes for better control of exceptions.

    
answered by 24.04.2018 / 17:34
source
0

You do not have to convert anything, the hours are already conceptually a whole value. Computers store dates (in most cases) such as the number of seconds since midnight UTC on January 1, 1970 .

For that C # offers the structure DateTime , if you want to save it in the database you can pass a DateTime to integer with the property Ticks :

long ahora = DateTime.Now.Ticks; // Guarda 'ahora' en base de datos.

To recover the data just do the reverse process:

long dato = 0; // Lee 'dato' de la base de datos.
var antes = new DateTime(dato).
    
answered by 24.04.2018 в 17:27