Jump to content

Return last 4 numerals from alpha-numeric variable?


Stack

Recommended Posts

I have a "Date" variable that I need to use twice, but the second instance would only include the year portion. So for instance, if the user inputs "June 2010", I'd like it to appear as typed-in on the first instance, and then just "2010" on the second instance. I want to create a rule for the second instance to do this, but I haven't found any examples for how to do this. Assuming there may be instances where the user includes a day in the date (e.g. "June 26, 2010"), I think the best approach would be to create a rule that trims the field and removes all but the last 4 characters, as opposed to stripping out all alpha and punctuation characters. Is this possible?

 

Additionally, I can foresee instances where the user may inadvertently press the space bar after typing the date, or copy/paste the date and unknowingly include a space or hard return ("June 26, 2010 "). If a trim code is possible, is it also possible to cut out any trailing spaces without including them as part of the 4 digit character count?

 

Thanks!

Link to comment
Share on other sites

var date = "July, 10 2010 ";
return Right(Trim(date),4);

 

The Trim function will remove any additional space from the "date" field and the Right function will start at the right of a string and return a given number of characters.

Link to comment
Share on other sites

var date = "July, 10 2010 ";
return Right(Trim(date),4);

The Trim function will remove any additional space from the "date" field and the Right function will start at the right of a string and return a given number of characters.

 

 

Thanks for the reply Step... I'm unsure how to implement this rule though, specifically this part:

 

var date = "July, 10 2010 ";

It seems that the code is correctly trimming the "July, 10 2010 " text as specified, NOT the date in my Date variable field. How would I incorporate the Date variable into the code, and not simply specify the date beforehand.

Link to comment
Share on other sites

The title of this thread suggests that the input data is an arbitrary "alpha-numeric variable," but you're obviously talking specifically about dates. And simply taking the last four characters makes a lot of assumptions about the input string, trailing spaces being just one of many potential problems. So instead of re-inventing this wheel, I would use FusionPro's built-in DateFromString function to parse the input string into a Date object, and then either FormatDate or Date.getFullYear() to return the year from that. For instance:

var myDate = DateFromString(Field("YourFieldName"));
return myDate.getFullYear();

Link to comment
Share on other sites

Ah, sorry for the confusion...

 

Dan, I tried the code you provided, but I get the following error: "uncaught exception: Error in function DateFromString: Could not convert "June 2010" to a valid date". This is just a shot in the dark, but could that be due to the fact that in this record, the Date would just be a month and year, not month, day and year? The format of the Date variable will always be alpha-numeric, in that the month will always be spelled out. And more often than not, it'll just be a month and year. But there may be instances where a day is thrown in as well, and of course the looming possibility of a trailing space or other potential problem, as you mentioned.

Link to comment
Share on other sites

Ah, sorry for the confusion...

 

Dan, I tried the code you provided, but I get the following error: "uncaught exception: Error in function DateFromString: Could not convert "June 2010" to a valid date". This is just a shot in the dark, but could that be due to the fact that in this record, the Date would just be a month and year, not month, day and year? The format of the Date variable will always be alpha-numeric, in that the month will always be spelled out. And more often than not, it'll just be a month and year. But there may be instances where a day is thrown in as well, and of course the looming possibility of a trailing space or other potential problem, as you mentioned.

Right, simply saying "June 2010" is not specific enough for JavaScript to turn into a valid Date object. There are several ways around this. I would download the excellent Datejs library from Google, and either copy-and-paste the code from the Date.js file into your JavaScript Globals, or copy the file into your Plug-ins folder. Then Date.parse will work with "partial" date specifications, as well as other nifty syntax. You'll want to add your own error handling on top of that as well, like so:

var myDate = Date.parse(Field("YourFieldName"));
if (!myDate)
   return "Invalid date!";
return myDate.getFullYear();

Link to comment
Share on other sites

Having said all that, this might be a simpler solution:

return Field("YourFieldName").match(/\d{4}/) || "Invalid date!";

This returns the first string of four digits found in the field value.

 

*HUGE SIGH OF RELIEF*

 

I'm glad for that followup post, haha... I'm not familiar with the JavaScript Globals, and most of the system folders on our Macs are accessible to admin only, so the plug-ins folder is off limits. I tried the above code and it worked perfectly. Thanks for your help!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...