Created
March 25, 2014 00:24
-
-
Save bennadel/9752372 to your computer and use it in GitHub Desktop.
Ask Ben: Cleaning Two Digit Years Using Javascript And Regular Expressions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This takes a date string that MIGHT have a two digit year | |
// as the last two digits. If it does, this function replaces | |
// the two digit year with what it *assumes* is the proper | |
// four digit year. | |
function CleanDate( strDate ){ | |
// Return the cleaned date. | |
return( | |
strDate.replace( | |
// This regular expression will search for a slash | |
// followed by EXACTLY two digits at the end of | |
// this date string. The two digits are being | |
// grouped together for future referencing. | |
new RegExp( "/(\\d{2})$", "" ), | |
// We are going to pass the match made by the | |
// regular expression off to this function literal. | |
// Our arguments are as follows: | |
// $0 : The entire match found. | |
// $1 : The first group within the match. | |
function( $0, $1 ){ | |
// Check to see if our first group begins with | |
// a zero or a one. If so, replace with 20 else | |
// replace with 19. | |
if ($1.match( new RegExp( "^[01]{1}", "" ) )){ | |
// Replace with 20. | |
return( "/20" + $1 ); | |
} else { | |
// Replace with 19. | |
return( "/19" + $1 ); | |
} | |
} | |
) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment