subtract mysql old time from now time using java script new Date function

I have imported time from MySQL and I want to subtract it from now time using java script,
the code I have used below, but the result I got is “invalid Date”. you can see my time table below.

 setInterval(function(){
    $(document).ready(function () {
        $.ajax({
       url: "plate.php",
       method:"GET",
        dataType:"JSON",
       success: function (data) {
           var id = [];
           var Time = [];
           var tp5 = [];
           for (var i in data){
              var oldtime = ((data[i].Time)); 
              console.log(data[i].id);
              console.log(data[i].tp5); 
              var nowtime= new Date();
              console.log(today);  
              var newDateObj = new Date(nowtime- oldtime);
            document.getElementById("datetime").innerHTML = newDateObj;
          }
        }
    });
});
        },  6000);

subtract mysql old time from now time using java script new Date function

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

oldtime should be a JS Date object.

Change:

var oldtime = ((data[i].Time));

To:

// date and time parts
let odp = data[i].Date.split("-");
let otp = data[i].Time.split(":");

let oldDate = new Date(odp[0], odp[1], odp[2], otp[0], otp[1], otp[2]);

Then you can get the difference between them with getTime


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x