0
I have the standard hours that the employee hits point. Ex:
8:00 - 12:00 - 13:00 - 18:00
And I have the time that he clocked in that day. Ex:
8 - 12:06 - 12:59 - 17:0
I need to calculate how many hours and minutes were left or added that day.
0
I have the standard hours that the employee hits point. Ex:
8:00 - 12:00 - 13:00 - 18:00
And I have the time that he clocked in that day. Ex:
8 - 12:06 - 12:59 - 17:0
I need to calculate how many hours and minutes were left or added that day.
3
You can create a function to do the Parsing of each of the time strings, and then calculate the amount of minutes between them:
function parse(horario) {
// divide a string em duas partes, separado por dois-pontos, e transforma em número
let [hora, minuto] = horario.split(':').map(v => parseInt(v));
if (!minuto) { // para o caso de não ter os minutos
minuto = 0;
}
return minuto + (hora * 60);
}
// calcula a duração total em minutos
function duracao(entrada1, saida1, entrada2, saida2) {
return (parse(saida1) - parse(entrada1)) + (parse(saida2) - parse(entrada2));
}
// duração de uma jornada normal de trabalho (em minutos)
let jornadaNormal = duracao('8:00', '12:00', '13:00', '18:00');
// quantidade de minutos efetivamente trabalhados
let jornada = duracao('8', '12:06', '12:59', '17:00');
// diferença entre as jornadas
let diff = Math.abs(jornada - jornadaNormal);
if (diff != 0) {
let horas = Math.floor(diff / 60);
let minutos = diff - (horas * 60);
console.log(`${horas} horas e ${minutos} minutos a ${jornada > jornadaNormal ? 'mais' : 'menos'}`);
}
With this, the code prints the amount of hours and minutes worked, and shows whether it is more (or less) than the normal workday:
0 hours and 53 minutes less
Another alternative is to use the Moment js., which has durations support, through Durations
.
The idea is similar:
function duracaoTotal(entrada1, saida1, entrada2, saida2) {
let formatos = ['HH:mm', 'HH']; // formato do horário pode ser com ou sem os minutos
let inicio = moment(entrada1, formatos);
let fim = moment(saida1, formatos);
// diferença entre a hora inicial e final
let diff = moment.duration(fim.diff(inicio));
inicio = moment(entrada2, formatos);
fim = moment(saida2, formatos);
// obter diferença entre a nova hora inicial e final e somar ao valor anterior
diff.add(moment.duration(fim.diff(inicio)));
return diff;
}
// duração de uma jornada normal de trabalho
let jornadaNormal = duracaoTotal('8:00', '12:00', '13:00', '18:00');
// duração efetivamente trabalhada
let jornada = duracaoTotal('8', '12:06', '12:59', '17:00');
// diferença entre as jornadas
let diff = jornada.subtract(jornadaNormal);
if (diff.asMinutes() != 0) {
// imprimir a quantidade de horas e minutos
console.log(`${Math.abs(diff.hours())} horas e ${Math.abs(diff.minutes())} minutos a ${diff.asMinutes() > 0 ? 'mais' : 'menos'}`);
}
<script src="https://momentjs.com/downloads/moment.min.js"></script>
Remember that both codes above assume that all times refer to the same day. If you have times on different days, you should explicitly put the dates in the strings (similar to what was done in the reply from André).
0
You can calculate the difference of the dates so the result will be in milliseconds
var diff = Math.abs(date1 - date2);
After this just create a Date object for this.
For example:
var diff = Math.abs(DataInicial - DataFinal);
After that you can put in the new Date that should work.
0
A very good library for handling dates is Moment.js
For example
const first = moment('03/05/2019 08:00', 'DD/MM/YYYY HH:SS');
const second = moment('03/05/2019 12:00', 'DD/MM/YYYY HH:SS');
second.diff(first, 'hour'); // 4
second.diff(first, 'minute') // 240
Browser other questions tagged javascript angularjs typescript
You are not signed in. Login or sign up in order to post.
but I need the time and not the date
– Maria