/ Published in: JavaScript
Add either 19 or 20 depending on the current year + 10
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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);"