Convert sum of minutes to hour format (ex: 70min = 01:10) with jquery
I have the following script that takes the hours and minutes of each day of the week, where the variables that start with m
are the minutes of each day of the week (coming from a select) and those that start with h
are the hours.
I'm taking the hour values, multiplying by 60 to have the minutes and adding up with all the minutes and I have at the end, the summation in minutes.
How do I convert these minutes into hours?
Ex: 70minutes would result 01: 10
<script>
$(document).ready(function(){
$("select").change(function(){
//VARIAVEIS MINUTOS
var mseg = parseInt($("#mseg").val());
var mter = parseInt($("#mter").val());
var mqua = parseInt($("#mqua").val());
var mqui = parseInt($("#mqui").val());
var msex = parseInt($("#msex").val());
var msab = parseInt($("#msab").val());
var mdom = parseInt($("#mdom").val());
var totalminutos = mseg + mter + mqua + mqui + msex + msab + mdom;
//VARIAVEIS HORAS
var hseg = parseInt($("#hseg").val());
var hter = parseInt($("#hter").val());
var hqua = parseInt($("#hqua").val());
var hqui = parseInt($("#hqui").val());
var hsex = parseInt($("#hsex").val());
var hsab = parseInt($("#hsab").val());
var hdom = parseInt($("#hdom").val());
var totalhoras = (hseg + hter + hqua + hqui + hsex + hsab + hdom) * 60;
var total = (totalhoras + totalminutos);
$("#total").val(total);
});
});
</script>
0
1 answers
You can create a function that will receive the value in minutes, use the function Math.floor
to remove the integer part, and a mod
(operator %
) to get the rest of the division by 60:
const converter = (minutos) => {
const horas = Math.floor(minutos/ 60);
const min = minutos % 60;
const textoHoras = (`00${horas}`).slice(-2);
const textoMinutos = (`00${min}`).slice(-2);
return `${textoHoras }:${textoMinutos}`;
};
console.log(converter(70));
2
Author: Sorack, 2019-03-08 19:18:42