Return to Snippet

Revision: 41780
at February 23, 2011 08:18 by lance


Initial Code
function yearAdd(inDate, id) {
            if (inDate == "") {
                return;
            }
            var dateToCheck = inDate;
            var thisDate = new Date();
            var thisYear = thisDate.getFullYear().toString();
            var thisyr = thisYear.substr(2, 2)
            var yearToNum = parseInt(thisyr);
            var yr = dateToCheck.split('/')[2];
            if (yr.length == 2) {
                yr = (yr > (yearToNum+10)) ? '19' + yr : '20' + yr;

                dateToCheck = dateToCheck.split('/'); dateToCheck[2] = yr; dateToCheck = dateToCheck.join('/');
                document.getElementById(id).value = dateToCheck;
            }
        }



use in input/text field

onblur="javascript:yearAdd(this.value, this.id);"

Initial URL

                                

Initial Description
Add either 19 or 20 depending on the current year + 10

Initial Title
Fill 4 digit year from 2 digits

Initial Tags

                                

Initial Language
JavaScript