Hey Ben,
Trying to replace a date "/" character with both the character and either a string of 19 or 20, for 2 year purposes.
For example, people are accustomed to plugging in the dates into their form with mm/dd/yy format. So when they plug in 12/23/97, I'd like to replace the second instance of / with /19 preceeding the 97.
If the last two year figures begin with 0 or 1, I'd replace the "/" with "/20" preceeding the rest of the figure. i.e. 09/17/02 would become 09/17/2002.
Is there a way to replace this with regular expressions? How would that be done?
The beauty of using Regular Expressions in Javascript is that each match made by the regular expression can be passed off to a Javascript function literal. You won't see this happening in any kind of compiled language such as Java or ColdFusion, which is part of why Javascript, as an interpreted language, is so freakin' sweet-ass!
For this solution, all we have to do is write a function that takes a given date string, checks for a two digit year, check the leading character and then returns the two digits with the proper two digits pre-pended.
Launch code in new window » Download code as text file »
Hope that helps!
Download Code Snippet ZIP File
Comments (3) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
String Tokenizer ColdFusion Component That Can Handle Qualified Fields
Parsing CSV Values Using A Standard File Format
Having users enter a two digit year is part of the Y2K issue. A common solution for 2 year digit data was to use a pivot formula, for the below example, I set 70 as the pivot year.
Easy enough to set the pivot base, depending on the type of question being asked, such as your birth date, and then depending on the age of your user base, lower the pivot. For me, entering in 61 would result in 2061, so I'd probably lower the pivot date a bit.
<script language="javascript">
function getPivot(year) {
if (year < 70) return (2000 + year);
if (year < 1900) return (1900 + year);
return year;
}
function getDate(){
var myDate = new Date(document.getElementById('testDate').value);
var pivotDate = new Date(getPivot(myDate.getYear()),myDate.getMonth(),myDate.getDate());
alert(pivotDate);
}
</script>
<input id="testDate" value="" onchange="getDate();" />
Posted by Christopher Wigginton on Jan 26, 2007 at 9:46 AM
Chris,
That's a cool little snippet of code there. Thanks!
Posted by Ben Nadel on Jan 26, 2007 at 6:11 PM
Ben,
your example is pretty nifty. I didn't realize that you can do the equivalent of the matchEvaulator in .NET but with Java/CF Script! Nice! On a more relevant note do deal with the issue I usually just provide a calendar widget. The one at http://www.dynarch.com/projects/calendar/ is free, full of features and customizable. I use it all the time. A quick question for you, why did you have to escape the "\" in front of the special sequence "\d" as in "\\d"?
On a side note, you never got back to me about the code snippets idea.
Posted by Boyan on Jan 28, 2007 at 8:41 PM