Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q1436 How can I show the number of days, hours and seconds till Y3K?

You are here: irt.org | FAQ | JavaScript | date | Q1436 [ previous next ]

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>

©2018 Martin Webb