Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

127
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!