I need to calculate the duration between two datetimes in JavaScript. I tried this code:
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;
var yyyy = today.getFullYear();
if(dd<10){dd='0'+dd} if(mm<10){mm='0'+mm} today = mm+'/'+dd+'/'+yyyy;
console.log("current date"+today);
var valuestart ="8:00 AM";
var valuestop = "4:00 PM";
var timeStart = new Date("01/01/2007 " + valuestart).getHours();
var timeEnd = new Date("01/01/2007 " + valuestop).getHours();
var hourDiff = timeEnd - timeStart;
console.log("duration"+hourDiff);
From this I can get the current date and duration. But when I replace the date "01.01.2007" with the variable "today", I get the result as NaN. Please lead me to where I am wrong. Thanks in advance.
source
share