Convert hours to minutes

1

I would like to convert a data in horas format in minutes using JQuery

Data to convert

  var hora='3:19:00';

result

  var nuevoDato= '199';
    
asked by Soldier 01.02.2018 в 23:56
source

3 answers

0

A different way of doing it would be to use JavaScript dates, although is not ideal because it has quite a few limitations. For example: the format of the text must be perfect (following the pattern hh:mm:ss ) and the hours must be valid (you can not have more than 23 hours or 59 minutes).

The idea would be to use an object of type Date to pause and work with time:

// hora a convertir
var t = "03:19:00";

// creamos una fecha genérica con tu tiempo
var d = new Date("0001-01-01T"+t);

// calculamos los minutos a partir de las horas y minutos de la fecha creada
var minutos = d.getHours() * 60 + d.getMinutes();

console.log(minutos);
    
answered by 18.02.2018 / 02:13
source
1

You can separate the parts with split and do the calculation from there:

//dato a convertir
var hora='3:19:00';

// Dividir en partes
var parts = hora.split(':');

// Calcular minutos (horas * 60 + minutos)
var total = parseInt(parts[0]) * 60 + parseInt(parts[1]);
  
console.log(total);
    
answered by 02.02.2018 в 00:02
1

with the java script split option you separate the data by minutes, seconds and hours and perform the conversion

<script type="text/javascript">

var pizza = "02:34:56";
var porciones = pizza.split(':');
document.write(porciones[0]);
document.write(porciones[1]);
document.write(porciones[2]); 
</script>
    
answered by 02.02.2018 в 04:26