Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare two Time stamps in minutes JS

Hi I'm using this timestamp:

function getTimeStamp() {
       var now = new Date();
       return ((now.getMonth() + 1) + '/' + (now.getDate()) + '/' + now.getFullYear() + " " + now.getHours() + ':'
                     + ((now.getMinutes() < 10) ? ("0" + now.getMinutes()) : (now.getMinutes())) + ':' + ((now.getSeconds() < 10) ? ("0" + now
                     .getSeconds()) : (now.getSeconds())));
}

and saving pause time here:

localStorage["TmpPause"] = getTimeStamp();

than after a while read time and compare:

var pauseTime = localStorage["TmpPause"];
    var resumeTime = getTimeStamp();

    var difference = resumeTime.getTime() - pauseTime.getTime(); // This will give difference in milliseconds
    var resultInMinute = Math.round(difference / 60000);

    alert(resultInMinute);

at the moment I'm unable to calculate the difference in between 2 dates including time. I'm getting error undefined is not a function resumeTime.getTime() ???

Thanks for your help.

like image 256
user3113040 Avatar asked Feb 14 '23 01:02

user3113040


1 Answers

getTimeStamp() does not return a timestamp (such irony), instead it returns a String in this format: "1/20/2014 19:18:28"

This is how you get a timestamp in millisecond: Date.now(), or even Date().toString() (time represented in text).

You should store this instead so that you can reuse it later on by doing:

new Date(yourTimestamp);  //this returns the original Date object

(newTimestamp - oldTimestamp)/1000/60   //returns difference in minute
like image 185
Derek 朕會功夫 Avatar answered Feb 17 '23 03:02

Derek 朕會功夫