Seconds Between Two Dates
Here is my code...
function buildTimer(second,minute,hour,day,month)
{
if(getCookie('day') == "")
{
setCookie("month",month,1095);
setCookie("day",day,1095);
setCookie("hour",hour,1095);
setCookie("minute",minute,1095);
setCookie("second",second,1095);
}
var d = new Date();
var curr_month = d.getMonth();
if(month >= curr_month)
{
year = d.getFullYear();
}
else
{
year = d.getFullYear()+1;
}
var Bday = new Date(year, month, day, hour, minute, second, 0);
var Today = new Date();
var totaldays = daysBetween(Today, Bday);
var time = (totaldays*86400) + (hour*3600) + (minute*60) + second;
alert(time);
CreateTimer(time);
}
AND here is the function that I used...
function daysBetween(date1, date2) {
// The number of milliseconds in one day
var ONE_DAY = 1000 * 60 * 60 * 24
// Convert both dates to milliseconds
var date1_ms = date1.getTime()
var date2_ms = date2.getTime()
// Calculate the difference in milliseconds
var difference_ms = Math.abs(date1_ms - date2_ms)
// Convert back to days and return
return Math.round(difference_ms/ONE_DAY)
}
The code at the end alerts a certain number of seconds, but it is not even close to the time between the two dates.
What am I doing wrong?
Joe34
Junior Poster in Training
92 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
var time_difference_in_seconds = (Today.getTime() - Bday.getTime()) / 1000;
Airshow
WiFi Lounge Lizard
2,682 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372