How to calculate hours via reduce?

Asked

Viewed 82 times

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?

  • 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

  • So getTime(Date.calcHrTrab accepts Date objects and what you want is to return the number of worked milliseconds and add everything, correct?

  • 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?

  • 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.

  • 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

  • Put here the text (we help to format) of console.log(JSON.stringify(registros)); that I use this in the answer.

  • I won’t (I don’t want to :) copy this JSON from an image, then ask you to put it here.

  • 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

  • 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?

Show 5 more comments

1 answer

1


You can make it simpler in one .reduce, with some support functions. I strongly advise against adding methods to the Date prototype such as Date.calcHrTrab.

A suggestion would be so:

const registos = [{"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}];


// Funções de apoio
const tempoEntreDatas = (a, b) => {
  if (!a && !b) return 0;
  return new Date(b).getTime() - new Date(a).getTime();
}

const tempoDeRegisto = (registo) => {
  const HORA_MAX_DE_SAIDA = 24;
  const entrada = new Date(registo.entrada);
  const intervalo = new Date(registo.intervalo);
  const retorno = new Date(registo.retorno);
  let saida = registo.saida;
  if (!saida) {
    saida = new Date(
      entrada.getFullYear(),
      entrada.getMonth(),
      entrada.getDate(),
      HORA_MAX_DE_SAIDA, 0, 0
    );
  }
  const duracaoDia = tempoEntreDatas(entrada, saida);
  const duracaoIntervalo = tempoEntreDatas(intervalo, retorno);
  return duracaoDia - duracaoIntervalo;
};

const msParaHHMMSS = (ms) => [
  Math.floor((ms / (1000 * 60 * 60)) % 24),
  Math.floor((ms / (1000 * 60)) % 60),
  Math.floor((ms / 1000) % 60)
].map(nr => nr < 10 ? '0' + nr : nr).join(':');

// O reduce
const registosTrabalhoMS = registos.reduce(
  (soma, registo) => soma + tempoDeRegisto(registo), 0
);

console.log(`O registou somou ${msParaHHMMSS(registosTrabalhoMS)}`);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.