Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

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>

Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 6th July 2009. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2009 irt.org, All Rights Reserved.