I have a code that pulls a JSON response, and I need to compare times from now() and a previous when the timestamp was created. I am able to pull the info and have it display.

However,
when i try and get the comparison to work it just sits there and doesnt display anything.

Here is my code

    "use strict";
        var endDate = new Date();
        endDate.setHours(endDate.getHours());

        var startDate = new Date();
        startDate.setHours(- 12);

     api.call("Get", {
                "typeName": "DutyStatusLog",
                "search": {
               "fromDate" : startDate,
               "ToDate" : endDate,
               "statuses" :['ON']
                 }

              }, function(results){
                 console.log(results);
                 for(var i=0; i<results.length; i++){
                   var stTime = results[i].dateTime;
                   var status = results[i].status;
                   var driver = results[i].driver.id;
                   //stTime.getHours();
                   console.log("Driver "+ driver +" at time: ",stTime + " And Has The Status Of " + status);
                   var newDate = new Date();
                   //newDate.getHours();

                   if((stTime - newDate ) > 6){
                       console.log("Answers: ",answerThis);
                   }
                 }
              });

Yes, i Know answerThis is not defined right now, But I am trying to just get the comparison to work right now if its greater than 6 hrs

here is what is returned from the array:

+annotations: Array[1]
+coDrivers: Array[0]
dateTime: "2018-02-12T20:37:26.594Z" //this is what i want to subtract from newDate
+device: Object
distanceSinceLastValidCoordinate: 0
+driver: Object
editDateTime: "2018-02-12T20:39:09.829Z"
eventCode: 4
eventRecordStatus: 1
eventType: 1
id: "a8g2YKRG0lEu5X7fEjiGp7g"
+location: Object
malfunction: "None"
origin: "Manual"
sequence: "0000000000000677"
state: "Active"
status: "ON"
version: "0000000000044acd"

You are comparing a string with a js Date object.

First instantiate a Date object from your string Click Here and then you can make the comparison with Date.getTime() Click Here

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.