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

Q938 How can I keep two sets of dates, chosen from drop down select boxes a maximum of 17 days apart, if the first date automatically updates the second date to be 17 days past the first date?

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

Try:

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

function isDate(day,month,year) {
    var today = new Date();
    var test = new Date(year,month,day);
    if ( (y2k(test.getYear()) == y2k(year)) && (month == test.getMonth()) && (day == test.getDate()) )
        return true;
    else
        return false
}

function validate(what,number) {
    var day1   = what.day1.options[what.day1.selectedIndex].text - 0;
    var month1 = what.month1.selectedIndex;
    var year1  = what.year1.options[what.year1.selectedIndex].text - 0;
    var day2   = what.day2.options[what.day2.selectedIndex].text - 0;
    var month2 = what.month2.selectedIndex;
    var year2  = what.year2.options[what.year2.selectedIndex].text - 0;
    if (number == 1) {
        if (isDate(day1,month1,year1)) {
            var date2 = new Date(year1,month1,day1 + 17);
            day2 = date2.getDate();
            month2 = date2.getMonth();
            year2 = y2k(date2.getYear());
            what.day2.options[day2-1].selected = true;
            what.month2.options[month2].selected = true;
            for (var i=0,l=what.year2.options.length;i<l;i++) {
                if (what.year2.options[i].text == year2) {
                    what.year2.options[i].selected = true;
                }
            }
        }
        else
            alert(day1 + '/' + (month1+1) + '/' + year1 + ' is not a valid date');
    }
    else {
        if (isDate(day2,month2,year2)) {
            var difference = (Date.UTC(year2,month2,day2,0,0,0) - Date.UTC(year1,month1,day1,0,0,0))/1000/60/60/24;
            if (difference > 17 || difference < 0)
                alert(day2 + '/' + (month2+1) + '/' + year2 + ' is more than 17 days past ' + day1 + '/' + (month1+1) + '/' + year1);
        }
        else
            alert(day2 + '/' + (month2+1) + '/' + year2 + ' is not a valid date');
    }
}
//--></script>

<form>

<select name="day1" onChange="validate(this.form,1)">
<option>1<option>2<option>3
<option>4<option>5<option>6
<option>7<option>8<option>9
<option>10<option>11<option>12
<option>13<option>14<option>15
<option>16<option>17<option>18
<option>19<option>20<option>21
<option>22<option>23<option>24
<option>25<option>26<option>27
<option>28<option>29<option>30
<option>31
</select>

<select name="month1" onChange="validate(this.form,1)">
<option>January
<option>February
<option>March
<option>April
<option>May
<option>June
<option>July
<option>August
<option>September
<option>October
<option>November
<option>December
</select>

<select name="year1" onChange="validate(this.form,1)">
<option>1995<option>1996<option>1997
<option>1998<option>1999<option>2000
<option>2001<option>2002<option>2003
<option>2004<option>2005
</select>


<select name="day2" onChange="validate(this.form,2)">
<option>1<option>2<option>3
<option>4<option>5<option>6
<option>7<option>8<option>9
<option>10<option>11<option>12
<option>13<option>14<option>15
<option>16<option>17<option>18
<option>19<option>20<option>21
<option>22<option>23<option>24
<option>25<option>26<option>27
<option>28<option>29<option>30
<option>31
</select>

<select name="month2" onChange="validate(this.form,2)">
<option>January
<option>February
<option>March
<option>April
<option>May
<option>June
<option>July
<option>August
<option>September
<option>October
<option>November
<option>December
</select>

<select name="year2" onChange="validate(this.form,2)">
<option>1995<option>1996<option>1997
<option>1998<option>1999<option>2000
<option>2001<option>2002<option>2003
<option>2004<option>2005
</select>

</form>

©2018 Martin Webb