Here I have a function which I use to calculate the difference between two Time variables, start and end, and display the output as ‘Past’, ‘Current’, or ‘Future’.
I was able to calculate ‘Current’ and ‘Future’ values, but no luck with “Past’. Here is the jsFiddle and any help would be great.
On a side note, I do prefer a plugin-less way to achieve this.
//My Start time and End time
var start = "Mon Nov 22 2012 08:45:00 GMT+0800 (Malay Peninsula Standard Time)";
var end = "Mon Nov 22 2012 10:15:00 GMT+0800 (Malay Peninsula Standard Time)";
//Calling to test function
test(start, end);
//Function to calculate
function test(startTime, endTime) {
startTime= new Date(startTime);
endTime= new Date(endTime);
var currentTime = new Date();
var difference = currentTime - startTime;
if (difference > 0 && currentTime > startTime) {
alert ("Past!");
}
if(startTime < currentTime && currentTime < endTime){
alert("Current!");
}
if(difference < -1){
alert("Future!");
}
}
Link to jsFiddle
Sigh!! I just need some sleep. It’s working and here’s the updated jsFiddle: http://jsfiddle.net/9jk6a/9/
Thanks everyone who helped.