Copy link to clipboard
Copied
I've been searching to see if this was already answered on this forum or elsewhere, but I'm not finding it with the search terms I've tried...
When typing in the Date Fields, it takes the first number you enter as the day, instead of the month, but I would like the first number entered to be used as the month.
This wouldn't be a problem if I wanted it to display as MM/DD/YYYY, because it doesn't switch the order of the numbers, so even if I type 2/1/24 & it thinks 2 is the day, it still displays at DD/MM/YYYY & keeps it looking as 2/1/2024.... but I have a custom date format set to show it as "ddd - mmm d" - so if I type 2/1 thinking it will be Feb 1, it puts it as Jan 2.... ("Tue - Jan 2" instead of "Thu - Feb 1")
What can I do to make it so that when I type 2/1, it shows it as Feb 1, not Jan 2?
Thanks!
Copy link to clipboard
Copied
You can try this:
function reverseDate(str) {
var regex = /^(\d{1,2})\/(\d{1,2})(\/(\d{2,4}))?$/;
var match = str.match(regex);
if (match) {
var month = match[1];
var day = match[2];
var year = match[4] || '';
if (year) {
return day + '/' + month + '/' + year;}
else {
return day + '/' + month;}}
else {
return str;}}
event.value = reverseDate(event.value);
Copy link to clipboard
Copied
This reply was originally to correct the typos in my original post, but I now have the option to edit my posts, so I edited my OP to remove the typos & this reply is no longer needed.
Copy link to clipboard
Copied
It should be "Thu - Feb 1" not "Thu - Feb 2".
If you want to keep that format you could use a Validate script to reverse the order of numbers, for example if you enter 2/1 it will reverse, so the value would be 1/2.
Copy link to clipboard
Copied
Yes, that was a typo, thanks - I fixed it in my OP.
What is the script that would reverse the numbers? You're saying that there's a script that if I typed in 2/1 it would reverse it behind the scenes to be 1/2, then it would still outbook the format I put in for it to show "Thu - Feb 1"?
Copy link to clipboard
Copied
Use this as 'Validate' script of that field:
function reverseDate(str) {
var parts = str.split("/");
return parts.reverse().join("/");}
event.value = reverseDate(event.value);
Copy link to clipboard
Copied
I'm trying to user-proof my form... some users may type in 2/1, others 2/1/24, others, 02/01/24... with your validate script, what will happen if they type in other formats of the date?
Copy link to clipboard
Copied
Could there be a more complicated switch, where if you typed in M/D, M/D/YY, MM/DD/YY, M/D/YYYY... it would switch what it believes is the M with the D?
Copy link to clipboard
Copied
The script will only work for M/D format, more complex script can be done to check for other formats but what if someone actually enter correct format D/M or D/M/YY, script can't know it's the right format.
Copy link to clipboard
Copied
I thought about that, but I can't deal with both scenarios. For the most part I think people will use M/D, because that is typical in the US or at least where I live, where people will be using this form. If people do enter D/M & hit tab to find that it shows a different month/day than expected, they would then probably realize they need to switch what they entered to M/D... but I'd like M/D to be the default for people to enter dates using this form in my geogragphical location. They also have the option to click on the calendar to select the date... but I'd like for ease of form entry to be able to type in the date & for it to default entry to M/D & also the possiblity of M/D/YYYY, MM/DD/YY, etc. (Our Excel works that way by default too, that if you enter 2/1 it's M/D, so Feb 1.)
Copy link to clipboard
Copied
.
Copy link to clipboard
Copied
You can try this:
function reverseDate(str) {
var regex = /^(\d{1,2})\/(\d{1,2})(\/(\d{2,4}))?$/;
var match = str.match(regex);
if (match) {
var month = match[1];
var day = match[2];
var year = match[4] || '';
if (year) {
return day + '/' + month + '/' + year;}
else {
return day + '/' + month;}}
else {
return str;}}
event.value = reverseDate(event.value);
Copy link to clipboard
Copied
Once again, you've helped with the perfect solution - thanks so much!! With that validate script, it puts out "Thu - Feb 1" no matter whether I enter 2/1, 2/1/24, 02/01/24, 2/1/2024, etc!
What's actually cool is, if someone normally does type the day first D/M, if they want to be able to do that & for it to come out right, they can type it as D-M... so with this validation script, if someone wants 2/1 to mean Feb 1st, but someone else wants 1-2 to mean Feb 1st, they can do that. 🙂
Copy link to clipboard
Copied
Hi Nesa, I just found an issue... I just tried entering a date with 2 digits for the day & it gave me an error... I tried entering "3/24" (which should be March 24th, so it would ultimately output "Sun - Mar 24" with the JS & my field format) & when I hit tab, it gave me an error that it was not the correct format! I guess I only tested it with dates that had 1 digit for the month & day, so I didn't realize that issue until I tried to enter a date with 2 digits for the day! Any thoughts? Thanks!
Copy link to clipboard
Copied
Hi Nesa, what if the user using different seperated like dot or dash 12.9.2024 or 12-9-2024 I tested the code and working well only with "/" Any advise? Thanks so much!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now