Results 1 to 4 of 4

Thread: CountDown to Date

  1. #1
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,126
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default CountDown to Date

    So I have this code that is counting down to a date June 28th 7pm (unspecified in this code 2011 and 7:30pm, 1/2 not so important). After June 28th though I'm guessing it will restart the counting till next years June 28th, is this correct? If so does anyone know how I can correct this, or of a script out there that already does this? I had one a few months ago but lost it. If I'm incorrect about the recounting can someone explain how the JS knows to stop on June 28th 2011? Thanks. For the live example you can see it here http://chrisisdoingwhat.com/.

    Code:
    <SCRIPT type="text/javascript">
    
    // ****  Time Zone Count Down Javascript  **** //
    /*
    Visit http://rainbow.arch.scriptmania.com/scripts/
     for this script and many more
    */
    
    ////////// CONFIGURE THE COUNTDOWN SCRIPT HERE //////////////////
    
    var month = '6';     //  '*' for next month, '0' for this month or 1 through 12 for the month 
    var day = '28';       //  Offset for day of month day or + day  
    var hour = 19;        //  0 through 23 for the hours of the day
    var tz = -5;         //  Offset for your timezone in hours from UTC
    var lab = 'countdown';    //  The id of the page entry where the timezone countdown is to show
    
    function start() {displayTZCountDown(setTZCountDown(month,day,hour,tz),lab);}
    
        // **    The start function can be changed if required   **
    window.onload = start;
    
    ////////// DO NOT EDIT PAST THIS LINE //////////////////
    
    function setTZCountDown(month,day,hour,tz)  {
    	var toDate = new Date();
    	if (month == '*')
    		toDate.setMonth(toDate.getMonth() + 1);
    	else if (month > 0) { 
    		if (month <= toDate.getMonth())
    			toDate.setYear(toDate.getYear() + 1);
    		toDate.setMonth(month-1);
    	}
    	if (day.substr(0,1) == '+')  {
    		var day1 = parseInt(day.substr(1));
    		toDate.setDate(toDate.getDate()+day1);
    	} else {
    		toDate.setDate(day);
    	}
    	toDate.setHours(hour);
    	toDate.setMinutes(0-(tz*60));
    	toDate.setSeconds(0);
    	var fromDate = new Date();
    	fromDate.setMinutes(fromDate.getMinutes() + fromDate.getTimezoneOffset());
    	var diffDate = new Date(0);
    	diffDate.setMilliseconds(toDate - fromDate);
    	return Math.floor(diffDate.valueOf()/1000);
    	}
    function displayTZCountDown(countdown,tzcd) {
    	if (countdown < 0) 
    		document.getElementById(tzcd).innerHTML = '<img src="oregon_trail.jpg" alt="opps?" /><br />The adventures begun.'; 
    	else {
    		var secs = countdown % 60; 
    		if (secs < 10) 
    			secs = '0'+secs;
    		var countdown1 = (countdown - secs) / 60;
    		var mins = countdown1 % 60; 
    		if (mins < 10) 
    			mins = '0'+mins;
    		countdown1 = (countdown1 - mins) / 60;
    		var hours = countdown1 % 24;
    		var days = (countdown1 - hours) / 24;
    		document.getElementById(tzcd).innerHTML = days + " day" + (days == 1 ? '':'s') + ' ' +hours+ ':' +mins+ ':'+secs+'';
    		setTimeout('displayTZCountDown('+(countdown-1)+',\''+tzcd+'\');',999);
    	}
    }
    </SCRIPT>
    Corrections to my coding/thoughts welcome.

  2. #2
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,126
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    I tried doing this way and it broke.

    Code:
    <SCRIPT type="text/javascript">
    
    // ****  Time Zone Count Down Javascript  **** //
    /*
    Visit http://rainbow.arch.scriptmania.com/scripts/
     for this script and many more
    */
    
    ////////// CONFIGURE THE COUNTDOWN SCRIPT HERE //////////////////
    
    var month = '6';     //  '*' for next month, '0' for this month or 1 through 12 for the month 
    var day = '28';       //  Offset for day of month day or + day  
    var hour = 19;        //  0 through 23 for the hours of the day
    var tz = -5;         //  Offset for your timezone in hours from UTC
    var lab = 'countdown';    //  The id of the page entry where the timezone countdown is to show
    var year = '2011';
    
    function start() { 
    	displayTZCountDown(setTZCountDown(month,day,hour,year,tz),lab);
    }
    
        // **    The start function can be changed if required   **
    window.onload = start();
    
    ////////// DO NOT EDIT PAST THIS LINE //////////////////
    
    function setTZCountDown(month,day,hour,tz, year)  {
    	var toDate = new Date();
    	if (month == '*')
    		toDate.setMonth(toDate.getMonth() + 1);
    	else if (month > 0) { 
    		if (month <= toDate.getMonth())
    			toDate.setYear(toDate.getYear() + 1);
    		toDate.setMonth(month-1);
    	}
    	if (day.substr(0,1) == '+')  {
    		var day1 = parseInt(day.substr(1));
    		toDate.setDate(toDate.getDate()+day1);
    	} else {
    		toDate.setDate(day);
    	}
    	if (toDate.getFullYear()  == year)
    		year = false;
    	else
    		year = toDate.getFullYear() - year;
    	toDate.setHours(hour);
    	toDate.setMinutes(0-(tz*60));
    	toDate.setSeconds(0);
    	var fromDate = new Date();
    	fromDate.setMinutes(fromDate.getMinutes() + fromDate.getTimezoneOffset());
    	var diffDate = new Date(0);
    	diffDate.setMilliseconds(toDate - fromDate);
    	return Math.floor(diffDate.valueOf()/1000), year;
    	}
    function displayTZCountDown(countdown,tzcd) {
    	if (countdown < 0  && !year) 
    		document.getElementById(tzcd).innerHTML = '<img src="oregon_trail.jpg" alt="opps?" /><br />The adventures begun.'; 
    	else {
    		var secs = countdown % 60; 
    		if (secs < 10) 
    			secs = '0'+secs;
    		var countdown1 = (countdown - secs) / 60;
    		var mins = countdown1 % 60; 
    		if (mins < 10) 
    			mins = '0'+mins;
    		countdown1 = (countdown1 - mins) / 60;
    		var hours = countdown1 % 24;
    		var days = (countdown1 - hours) / 24;
    		if (year)
    			document.getElementById(tzcd).innerHTML = year + " year" (year == 1 ? ' ':'s') + days + " day" + (days == 1 ? '':'s') + ' ' +hours+ ':' +mins+ ':'+secs+'';
    		else 
    			document.getElementById(tzcd).innerHTML = days + " day" + (days == 1 ? '':'s') + ' ' +hours+ ':' +mins+ ':'+secs+'';
    		setTimeout('displayTZCountDown('+(countdown-1)+',\''+tzcd+'\');',999);
    	}
    }
    </SCRIPT>
    Corrections to my coding/thoughts welcome.

  3. #3
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    Code:
    function setTZCountDown(month,day,hour,tz)  {
    	var toDate = new Date();
    	if (month == '*')
    		toDate.setMonth(toDate.getMonth() + 1);
    	else if (month > 0) {
    		if (month <= toDate.getMonth())
    			toDate.setYear(toDate.getFullYear() + 1);
    		toDate.setMonth(month-1);
    	}
    	if (day.substr(0,1) == '+')  {
    		var day1 = parseInt(day.substr(1));
    		toDate.setDate(toDate.getDate()+day1);
    	} else {
    		toDate.setDate(day);
    	}
    	toDate.setHours(hour);
    	toDate.setMinutes(0-(tz*60));
    	toDate.setSeconds(0);
    	var fromDate = new Date();
        if (toDate<fromDate){
         toDate.setYear(toDate.getYear()+1);
        }
    	fromDate.setMinutes(fromDate.getMinutes() + fromDate.getTimezoneOffset());
    	var diffDate = new Date(0);
    	diffDate.setMilliseconds(toDate - fromDate);
    	return Math.floor(diffDate.valueOf()/1000);
    	}
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  4. #4
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,126
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Thanks, but I don't see how this distinguish between the current year and a year in the future? Upon testing the original code further it appears to stop after the date and doesn't go forward to the next year, at that date so I assume I'm missing something with the JS date object.

    I recently found this code (unrelated except for the date object) and I was wondering how I could pass a day to it? Also if the hours passed 23 will it fail?

    Code:
    var h = 11, m = 22, s = 60;
    time_past = "1/1/1970 " + h + ":" + m + ":" + s;
    new Date( Date.parse( time_past ) )
    I tried

    Code:
    var h = 11, m = 22, s = 60, d =1;
    time_past = "1/" + d + "/1970 " + h + ":" + m + ":" + s;
    new Date( Date.parse( time_past ) )
    Thanks for any help.
    Corrections to my coding/thoughts welcome.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •