Posted By

lance on 02/23/11


Tagged


Versions (?)

Fill 4 digit year from 2 digits


 / Published in: JavaScript
 

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

  1. function yearAdd(inDate, id) {
  2. if (inDate == "") {
  3. return;
  4. }
  5. var dateToCheck = inDate;
  6. var thisDate = new Date();
  7. var thisYear = thisDate.getFullYear().toString();
  8. var thisyr = thisYear.substr(2, 2)
  9. var yearToNum = parseInt(thisyr);
  10. var yr = dateToCheck.split('/')[2];
  11. if (yr.length == 2) {
  12. yr = (yr > (yearToNum+10)) ? '19' + yr : '20' + yr;
  13.  
  14. dateToCheck = dateToCheck.split('/'); dateToCheck[2] = yr; dateToCheck = dateToCheck.join('/');
  15. document.getElementById(id).value = dateToCheck;
  16. }
  17. }
  18.  
  19.  
  20.  
  21. use in input/text field
  22.  
  23. onblur="javascript:yearAdd(this.value, this.id);"

Report this snippet  

You need to login to post a comment.