|
Q1436 How can I show the number of days, hours and seconds till Y3K?
irt.org | Knowledge Base | JavaScript | date | Q1436 [ previous next ]
Q1436 How can I show the number of days, hours and seconds till Y3K?
Try:
<script language="JavaScript"><!--
function timeTillDate(then) {
now = new Date();
thenTime = then.getTime();
nowTime = now.getTime();
var difference = thenTime-nowTime;
var daysDifference = Math.floor(difference/1000/60/60/24);
difference = difference - daysDifference*1000*60*60*24;
var hoursDifference = Math.floor(difference/1000/60/60);
difference = difference - hoursDifference*1000*60*60
var secondsDifference = Math.floor(difference/1000);
var string = '';
if (daysDifference > 0) {
string += daysDifference + ' day';
if (daysDifference > 1) string +='s';
string += ' ';
}
if (hoursDifference > 0) {
string += hoursDifference + ' hour';
if (hoursDifference > 1) string +='s';
string += ' ';
}
if (secondsDifference > 0) {
string += secondsDifference + ' second';
if (secondsDifference > 1) string +='s';
string += ' ';
}
return string;
}
document.write(timeTillDate(new Date(3000,0,1)));
//--></script>
|
|
|
Copyright © 1996-2008 irt.org, All Rights Reserved.