0

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?

Edited by Joe34: added alert()

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by Airshow
0

var time_difference_in_seconds = (Today.getTime() - Bday.getTime()) / 1000;

Edited by Airshow: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.