Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Weekly Javascript Countdown

Status
Not open for further replies.

marcandrew

Programmer
Feb 11, 2005
7
0
0
US
I've noticed a number of scripts that countdown to a specified date, but I am looking to countdown to a certain weekly. (Ex: 5 Days 3 hours and 2 mintues until the next show.) a coutner like this would be shown weekly until Monday 5pm.

any help?
 
woo, fun.
thanks!

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
	<title>Untitled</title>
	
<script type="text/javascript"><!--

function updateTimeSpan() {

	var theSpan = document.getElementById('timeLeft');
	var d = new Date();
	var t = new Date();
	var ms;
	var s, m, h, d;
	
	// if it's after 5pm, set today to tomorrow
	if( d.getHours() > 16 )
		d.setDate( d.getDate() + 1 );
	
	// get the next monday
	while( d.getDay() != 1 )
	    d.setDate( d.getDate() + 1 );
	
	// set the time
	d.setHours( 17 );
	d.setMinutes( 0 );
	d.setSeconds( 0 );
	
	// get the difference between right now and next monday
	ms = d - t;
	
	// get the days between now and then
	d = parseInt( ms / ( 1000 * 60 * 60 * 24 ) );
	ms -= ( d * 1000 * 60 * 60 * 24 );
	
	// get hours
	h = parseInt( ms / ( 1000 * 60 * 60 ) );
	ms -= ( h * 1000 * 60 * 60 );
	
	// get minutes
	m = parseInt( ms / ( 1000 * 60 ) );
	ms -= ( m * 1000 * 60 );
	
	// get seconds
	s = parseInt( ms / 1000 );
	
	theSpan.innerHTML = d + ' days, ' + h + ' hours, ' + m + ' minutes, and ' + s + ' seconds.';

	setTimeout( 'updateTimeSpan()', 100 );
}

onload = updateTimeSpan;

//--></script>
	
</head>

<body>

Time until next Monday at 5:00 PM: <span id="timeLeft"></span>

</body>
</html>

*cLFlaVA
----------------------------
[tt]your mom goes to college[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
*cLFlaVA

Thank You so Much!!
Just what I was looking for.
 
I wanted to play too (we're all sad... very very sad)... [smile]
Code:
<html>
<head>
<title>Test Countdown Harness</title>
<script type="text/javascript">
<!--
var stopYear    = 2005;	/* the year (YYYY) to stop the count down */
var stopMonth   = 10;	/* the month (1-12) that we stop the count down */
var stopDay     = 11;	/* the day of the month (1-31) that we stop the count down */
var stopHour    = 17;	/* the number of hours (0-23) that we stop the count down*/
var stopMins    = 30;	/* the number of minutes (0-59) that we stop the count down */
var stopSeconds = 0;	/* the number of seconds (0-59) that we stop the count down */

/* build up the date that we are to stop the count down into a single date object */
var stopDate = new Date();
stopDate.setFullYear(stopYear,stopMonth-1,stopDay);
stopDate.setHours(stopHour,stopMins,stopSeconds);

function countDown() {
	var _milliseconds = stopDate - new Date();
	var _days = Math.round(_milliseconds/(86400000));
	_milliseconds = _milliseconds % (86400000);
	var _hours = Math.round(_milliseconds/(3600000));
	_milliseconds = _milliseconds % (3600000);
	var _minutes = Math.round(_milliseconds/(60000));
	var countDownText = 'The show is being played right now!';
	if (_minutes > 0) {
		countDownText = _days + ' Day' + (_days!=1 ? 's ' : ' ');
		countDownText += _hours + ' hour' + (_hours!=1 ? 's and ' : ' and ');
		countDownText += _minutes + ' minute' + (_minutes!=1 ? 's ' : ' ');
		countDownText += 'until the next show.';
	} else {
		if (countDownHandle) clearInterval(countDownHandle);
	}
	document.getElementById('countdown-span').innerHTML = countDownText;
}

var countDownHandle = null;
function startCountDown() {
	countDown();
	countDownHandle = setInterval('countDown()',60000);
}
window.onload = startCountDown;
//-->
</script>
</head>
<body>
<span id="countdown-span"></span>
</body>
</html>

Cheers for the challenge,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
Well, I got a little bored:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head><title>Timer</title>
<script type="text/javascript"><!--
function cTime()
{
today = new Date();
tempMon = 7-Math.abs(1 - today.getDay());
nMonday = (tempMon!=7||(tempMon==7&&today.getHours()>16))?new Date(Date.parse(new Date(today.getFullYear(),today.getMonth(),today.getDate(),17))+(tempMon*86400000)):today;
d = Math.floor((nMonday-today)/86400000);
h = Math.floor((nMonday-today-(d*86400000))/3600000);
m = Math.floor((nMonday-today-(d*86400000)-(h*3600000))/60000);
s = Math.round((nMonday-today-(d*86400000)-(h*3600000)-(m*60000))/1000);
document.getElementById("rTime").innerHTML = d+" days "+h+" hours "+m+" minutes "+s+" seconds remaining";
}
onload = cTime;
setInterval("cTime()",990);
--></script>
</head>
<body>
<span id='rTime'></span>
</body>
</html>

"It is the mark of an educated mind to be able to entertain a thought without accepting it." - Aristotle
 
Thank You all for being so helpful..I def learned alot from reviewing your scripts..

Thanks again everyone,

Marc
 
Had a problem with Jeffs script it showed me 3 days 24 hours ect. I'm
sad... very very sad
too.
Code:
<html>
<head>
<title>Test Countdown Harness</title>
<script type="text/javascript">
<!--
var stopYear    = 2005;    /* the year (YYYY) to stop the count down */
var stopMonth   = 10;    /* the month (1-12) that we stop the count down */
var stopDay     = 11;    /* the day of the month (1-31) that we stop the count down */
var stopHour    = 17;    /* the number of hours (0-23) that we stop the count down*/
var stopMins    = 30;    /* the number of minutes (0-59) that we stop the count down */
var stopSeconds = 0;    /* the number of seconds (0-59) that we stop the count down */

/* build up the date that we are to stop the count down into a single date object */
var stopDate = new Date();
stopDate.setFullYear(stopYear,stopMonth-1,stopDay);
stopDate.setHours(stopHour,stopMins,stopSeconds);

function countDown() {
    var _milliseconds = stopDate - new Date();
    var _days = Math.round(_milliseconds/(86400000));
    _milliseconds = _milliseconds % (86400000);
    var _hours = Math.round(_milliseconds/(3600000));
    _milliseconds = _milliseconds % (3600000);
    var _minutes = Math.round(_milliseconds/(60000));
    var countDownText = 'The show is being played right now!';

        
    if (_minutes > 0) {
	[COLOR=red]_hours==24 ? _days= _days+1: _days=_days;[/color]
     	countDownText = _days + ' Day' + (_days!=1 ? 's ' : ' ');
       	 countDownText += _hours + ' hour' + (_hours!=1 ? 's and ' : ' and ');
       	 countDownText += _minutes + ' minute' + (_minutes!=1 ? 's ' : ' ');
       	 countDownText += 'until the next show.';

    } else {
        if (countDownHandle) clearInterval(countDownHandle);
    }
    document.getElementById('countdown-span').innerHTML = countDownText;
}

var countDownHandle = null;
function startCountDown() {
    countDown();
    countDownHandle = setInterval('countDown()',60000);
}
window.onload = startCountDown;
//-->
</script>
</head>
<body>
<span id="countdown-span"></span>
</body>
</html>
Hope you don't mind Jeff


Glen
 
Sad Sad Sad I got to get a life.
Code:
<html>
<head>
<title>Test Countdown Harness</title>
<script type="text/javascript">
<!--
var stopYear    = 2005;    /* the year (YYYY) to stop the count down */
var stopMonth   = 10;    /* the month (1-12) that we stop the count down */
var stopDay     = 19;    /* the day of the month (1-31) that we stop the count down */
var stopHour    = 17;    /* the number of hours (0-23) that we stop the count down*/
var stopMins    = 30;    /* the number of minutes (0-59) that we stop the count down */
var stopSeconds = 0;    /* the number of seconds (0-59) that we stop the count down */

/* build up the date that we are to stop the count down into a single date object */
var stopDate = new Date();
stopDate.setFullYear(stopYear,stopMonth-1,stopDay);
stopDate.setHours(stopHour,stopMins,stopSeconds);

function countDown() {
    var _milliseconds = stopDate - new Date();
    var _days = Math.round(_milliseconds/(86400000));
    _milliseconds = _milliseconds % (86400000);
    var _hours = Math.round(_milliseconds/(3600000));
    _milliseconds = _milliseconds % (3600000);
    var _minutes = Math.round(_milliseconds/(60000));
    var countDownText = 'The show is being played right now!';

        
    if (_minutes > 0) {
	_hours==24 ? _days= _days+1: _days = _days;//gm
	[COLOR=red]_hours==24 ? _hours=0: _hours = _hours;//gm[/color]
     	countDownText = _days + ' Day' + (_days!=1 ? 's ' : ' ');
       	 countDownText += _hours + ' hour' + (_hours!=1 ? 's and ' : ' and ');
       	 countDownText += _minutes + ' minute' + (_minutes!=1 ? 's ' : ' ');
       	 countDownText += 'until the next show.';

    } else {
        if (countDownHandle) clearInterval(countDownHandle);
    }
    document.getElementById('countdown-span').innerHTML = countDownText;
}

var countDownHandle = null;
function startCountDown() {
    countDown();
    countDownHandle = setInterval('countDown()',60000);
}
window.onload = startCountDown;
//-->
</script>
</head>
<body>
<span id="countdown-span"></span>
</body>
</html>
;-)

Glen
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top