0
So I have a little doubt about doing the calculation of hours using reduce, because until the stage where I left I can return the hours in normal format and now I need to do the sum of them I thought to "break"in milliseconds, but do not know yet how to proceed, below the console code and print.
Note: the first line I only put because of context, this is part of the Node where I communicate with the database to return the user’s available records
$.get(`/usuarios/${value}/filtro/registros?year=${ano}&month=${mes}`, function(registros) {
let inconsistencias = [];
let horasTrabalhadasDia = [];
registros.forEach(function(re) {
var tste = getTime(Date.calcHrTrab(re.entrada, re.intervalo, re.retorno, re.saida))
console.log(tste)
if (tste == '----') {
return null;
}
//console.log(re)
horasTrabalhadasDia.push(tste);
});
console.log(horasTrabalhadasDia)
// var horasTrabalhadasDia = [];
// registros.forEach(function (re) {
// horasTrabalhadasDia.push(Date.calcHrTrab(re.entrada, re.intervalo, re.retorno, re.saida));
//console.log(horasTrabalhadasDia)
//});
//console.log(horasTrabalhadasDia)
var totalHorasMes = horasTrabalhadasDia.reduce((a, p) => a + p);
console.log(totalHorasMes)
//console.log(registros)
And the array of records is:
[
{
"cod": 55,
"cod_usuario": 3,
"data": "2019-09-04T13:06:38.000Z",
"entrada": "2019-09-04T13:06:38.000Z",
"intervalo": null,
"retorno": null,
"saida": "2019-09-04T13:07:54.000Z",
"justificativa_entrada": null,
"justificativa_saida": null,
"justificativa_intervalo": null,
"justificativa_retorno": null
},
{
"cod": 54,
"cod_usuario": 3,
"data": "2019-09-01T14:59:50.000Z",
"entrada": "2019-09-04T13:07:35.000Z",
"intervalo": null,
"retorno": null,
"saida": null,
"justificativa_entrada": null,
"justificativa_saida": null,
"justificativa_intervalo": null,
"justificativa_retorno": null
},
{
"cod": 52,
"cod_usuario": 3,
"data": "2019-08-28T16:39:48.000Z",
"entrada": "2019-08-28T16:39:43.000Z",
"intervalo": "2019-08-28T17:00:00.000Z",
"retorno": "2019-08-28T16:45:48.000Z",
"saida": null,
"justificativa_entrada": null,
"justificativa_saida": null,
"justificativa_intervalo": "Esqueceu de registrar o ponto",
"justificativa_retorno": "Esqueceu de registrar o ponto"
},
{
"cod": 51,
"cod_usuario": 3,
"data": "2019-08-27T15:22:56.000Z",
"entrada": "2019-08-27T15:22:56.000Z",
"intervalo": "2019-08-27T15:23:51.000Z",
"retorno": "2019-08-27T15:23:59.000Z",
"saida": "2019-08-27T15:24:06.000Z",
"justificativa_entrada": null,
"justificativa_saida": null,
"justificativa_intervalo": null,
"justificativa_retorno": null
}
]
This console image comes from where? can you give an example of a record and the return of
getTime(Date.calcHrTrab
?– Sergio
getTime(Date.calcHrTrab is a function I use to calculate the hours I return from the bank I think I have not explained very well regarding my system, it is something like an electronic point system I will send a link with the images to explain better. https://imgur.com/a/0r4lZrZ
– Tiago Abdalla
So
getTime(Date.calcHrTrab
accepts Date objects and what you want is to return the number of worked milliseconds and add everything, correct?– Sergio
So as you saw in the console above, I use the function
.reduce((a, p) => a + p);
so that I may sum up the hours returned, but as seen on the island, it ends up not adding up by being in the incorrect format it just concatenates and what I need in reality is to add up to give a final result that would be considered the hours worked what in the above case would give 00:22:35, understand?– Tiago Abdalla
Yes I understand, this is simple. I keep asking until I have something more concrete to give an answer that helps others as well. You can put here the result of
console.log(JSON.stringify(registros));
right in the line below the$.get
.– Sergio
first is the return of console.log(JSON.stringify(records)); and the second is a console.log(records) I made this look more organized than JSON https://imgur.com/a/lRjsLdK
– Tiago Abdalla
Put here the text (we help to format) of
console.log(JSON.stringify(registros));
that I use this in the answer.– Sergio
I won’t (I don’t want to :) copy this JSON from an image, then ask you to put it here.
– Sergio
This one, I can’t put much character in here. https://pastr.io/view/7SvRHq1hbPD This getting too long here, wouldn’t it be better to talk by email or something? I can’t chat here
– Tiago Abdalla
When the exit is
null
how to interpret this data? there is a maximum time? type 18.00, or midnight? or account for the next day?– Sergio