Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

133
Visualizações
How to correctly subtract the currentDate and the endTime(read from a json file)

currently I am trying to create a function to tell me if a date event provided in a json object, is finish or not, by subtracting the current day and the end Date, however it always result positive, not sure what I am doing wrong, or if I am parsing bad the info coming from the json, still I placed the string in "hard" to see if that will change something, but still having the same issue:

So my function looks like this:

 $(document).ready(function (e) {

        for (let i = 0; i < eventsObject.events.length; i++) {

            var startTime = new Date(eventsObject.events[i].begin).getTime();
            var endTime = new Date(eventsObject.events[i].end).getTime();
            var classId = "event-" + i;

            var now = new Date().getTime();
            var distance =   endTime - now;

            if (distance < 0) {
                console.log("The event number " + i + " ended")
            }
        }

    });

and my json Object looks like this:

var eventsObject = {
    "events": [
        {

            "begin": "Mar 10, 2022 17:10:00 UTC",
            "end": "Mar 10, 2022 17:13:00 UTC"

        },
        {

            "begin": "Mar 8, 2022 17:15:00 UTC",
            "end": "Mar 8, 2022 17:21:00 UTC"

        },
        {

            "begin": "Mar 9, 2022 17:25:00 UTC",
            "end": "Mar 9, 2022 17:30:00 UTC"

        },
        {

            "begin": "Mar 10, 2022 11:00:00 UTC",
            "end": "Mar 10, 2022 11:10:00 UTC"


        },
    ]
}

So in theory just one event should be finished(08.03.2022), based in my date zone, Central Europe. Or might be this the issue maybe? Thank in advance for the help.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

With MomentJs, it's easy!

var date1 = moment(); //moment(new Date())
var date2 = moment('Mar 10, 2022 17:10:00 UTC');
var diff = date2.diff(date1);

Also DateJs.

But maybe with Date.parse('Mar 10, 2022 17:10:00 UTC')

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda