Converting a dynamic 4-digit year to a 2 digit year using Javascript

javascript, jquery, regex

Solution

It's simple arithmetic:

year = year % 100;

But why do you want to do this? Don't you remember the Y2K problem?

Problem

I need to convert a dynamically generated date from something like 20-Apr-2013 to 20.04.13. So far I managed to convert the month and change the spacers. But converting the year still escapes me. here is what I came up with so far. How to move forward? ``` $(document).ready(function() { $('.date').each( function() { var oldDate = $(this).text(); var month; if( oldDate.indexOf('-') > 0 ){ var dateSplit = oldDate.split('-'); var year = dateSplit[2]; if( year.length == 2){ year = year; } switch(dateSplit[1]) { case 'Jan': month = "01"; break; case 'Feb': month = "02"; break; case 'Mar': month = "03"; break; case 'Apr': month = "04"; break; case 'May': month = "05"; break; case 'Jun': month = "06"; break; case 'Jul': month = "07"; break; case 'Aug': month = "08"; break; case 'Sep': month = "09"; break; case 'Oct': month = "10"; break; case 'Nov': month = "11"; break; case 'Dec': month = "12"; break; } $(this).text(dateSplit[0] + '.' + month + '.' + year); } else if( oldDate.indexOf(('/') > 0 ) ){ var dateSplit = oldDate.split('/'); var year = dateSplit[2]; if( year.length == 2){ year = year; } } }); }); ```

Original source