Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

122
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda