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

Q1309 How can I show all the dates between two dates in YYYY-MM-DD format?

You are here: irt.org | FAQ | JavaScript | Date | Q1309 [ previous next ]

Try:

<script language="JavaScript"><!--
function y2k(number) { return (number < 1000) ? number + 1900 : number; }
function padout(number) { return (number < 10) ? '0' + number : number; }

function showDates(startYear,startMonth,startDay,endYear,endMonth,endDay) {
    startDate = new Date(startYear,startMonth - 1,startDay);
    endDate = new Date(endYear,endMonth - 1,endDay);

    for (;;) {
        // infinite loop
        if (startDate > endDate) {
            // break out of infinte loop and function
            return;
        }
        document.write(y2k(startDate.getYear()) + '-' + padout(startDate.getMonth() + 1) + '-' + padout(startDate.getDate()) + '<br>');
        // add a day to the date:
        startDate = new Date(startDate.getTime() + 1*24*60*60*1000);
    }
}

showDates(1999,12,1,1999,12,25);
//--></script>

©2018 Martin Webb