Convert date entered as DD-MM-YY to DD-MM-YYYY

Convert date entered as DD-MM-YY to DD-MM-YYYY

  

In addition to being able to enter the standard MM-DD-YYYY date format, I need to allow the user to type in dates with the format MM-DD-YY (I have a separate JQuery widget performing a masking to guarantee the placement of hyphens and numbers) and then change the year to the full four digits. As per my user story, I am assuming for this problem that any year entered this way is in the 21st century.

I need to be able to reuse this for multiple date fields as part of onchange events. Therefore, I am trying to create an action to which I can pass the Id of the input field as well as the typed value of the field, verifying that it is in the format MM-DD-YY and that it can be made into a valid date, and then replacing the value of the input field with MM-DD-20YY. If the conditions of the action are not met, I would like to leave the user input as-is, so the user has a chance see their mistake and make corrections if necessary. 

Due to the re-usability factor, and rather than returning a date and replacing the value, I am looking at constructing some Javascript that will only execute iff the input follows the rules as described above.

I have the logic layed out to prevent the Javascript from executing, but I am having trouble with the Javascript itself.

Do I need to define this as a function on the webscreen and use the ExecuteJavaScript action to call the function?

Hi Cody,

If I remember correctly, if you're using RunJavaScript you just provide the Javascript code, no need for the <script> tag.

Jorge Martins wrote:

Hi Cody,

If I remember correctly, if you're using RunJavaScript you just provide the Javascript code, no need for the <script> tag.

This fixes another issue I was having with the page breaking after the OnChange action completed, but I am still not seeing a change in the date in the form.

In case it matters, I am passing the input widget's typed value rather than the date variable, since the date's value resolves to NullDate until it is assigned a date in the format MM-DD-YYYY. The typed value is then being accurately parsed for the respective (integer) date parts.

I am not very well versed in Javascript. Do I need to be using a different method of assigning the date value?

Update:
I was able to get the date to save by running the following JavaScript:

However, when I enter "10-11-12" in the input widget, the date being returned by the Javascript is in the written as "Thu Oct 11 2012 00:00:00 GMT-0400 (Eastern Daylight Time)". I need this to resolve to "10-11-2012"

Hi Cody,


You can try by changing the variable of Input Widget from Date to Text. and assigning  the control date like 

"document.getElementById('"+EncodeJavaScript(InPut.Id) + "').value = " + Day + "-" + (Month -1) + "-" + Year "

You also need to cast the variable back to Date while saving and need to implement custom validation for Date validation

Hope this should work


Regards

Devendra

Solution

Devendra Baghel wrote:

Hi Cody,


You can try by changing the variable of Input Widget from Date to Text. and assigning  the control date like 

"document.getElementById('"+EncodeJavaScript(InPut.Id) + "').value = " + Day + "-" + (Month -1) + "-" + Year "

You also need to cast the variable back to Date while saving and need to implement custom validation for Date validation

Hope this should work


Regards

Devendra

Here is the end JavaScript that ended up working:
"     document.getElementById('"+ EncodeJavaScript(InputId) +"').value = '"+ EncodeJavaScript(NewDateString) +"'; "

Note the single quotes surrounding the string to be passed.

After some frustration, I ended up constructing the date string in a separate assignment to lighten the amount of formatting that had to be done inside the JavaScript. I believe the formatting is what was causing the problems. All of the calculations are now handled before the Javascript, including the date construction.

I ended up being able to leave the variable as a date type, thankfully. I have several dates to use this action on, and adding custom validation for each one would have been far too time consuming.

Solution