Skip to main content
Known Participant
January 31, 2024
Answered

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

  • January 31, 2024
  • 1 reply
  • 1499 views

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!

This topic has been closed for replies.
Correct answer Nesa Nurani

 .


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

1 reply

Known Participant
January 31, 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.

Nesa Nurani
Community Expert
Community Expert
January 31, 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.

Known Participant
January 31, 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?


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?