Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
13

Date Field Entry uses first number entered as day, but I'd like it to be the month

Explorer ,
Jan 30, 2024 Jan 30, 2024

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!

TOPICS
JavaScript , PDF forms
1.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
Jan 30, 2024 Jan 30, 2024

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);

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 30, 2024 Jan 30, 2024

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 30, 2024 Jan 30, 2024

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 30, 2024 Jan 30, 2024

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"?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 30, 2024 Jan 30, 2024

Use this as 'Validate' script of that field:

function reverseDate(str) {
var parts = str.split("/");
return parts.reverse().join("/");}

event.value = reverseDate(event.value);

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 30, 2024 Jan 30, 2024

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 30, 2024 Jan 30, 2024

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 30, 2024 Jan 30, 2024

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 30, 2024 Jan 30, 2024

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.)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 30, 2024 Jan 30, 2024

 .

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 30, 2024 Jan 30, 2024

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);
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 30, 2024 Jan 30, 2024

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. 🙂

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 22, 2024 Feb 22, 2024

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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 09, 2024 Dec 09, 2024
LATEST

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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines