Copy link to clipboard
Copied
I have a form that needs to be in this date format - 4th of February 2022. It needs to have the "th" "nd" "st" or "rd" after the day. I found a formula that will allow this for today's date. However, I need the form user to be able to select the day - and not prefill with today's date.
Here is the script for today's date:
var oDate = new Date();var sDay = util.printd("d", oDate);var aSuffix = ["", "st", "nd", "rd", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th", "th", "st"];var sDD = sDay + aSuffix[+sDay];event.value = (sDD + util.printd(" of mmmm, yyyy", oDate)).toUpperCase();
I tried to fix it by entering this into the validation instead - but it's not pulling in the orinary number now:
var oDate = new Date();var aSuffix = ["", "st", "nd", "rd", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th", "th", "st"];var sDD = sDay + aSuffix[+sDay];event.value;
I honestly know nothing about Javascript and try to just fudge my way through it. Would someone be able to help me please?
Copy link to clipboard
Copied
Try this code as the field's custom Validation script:
if (event.value) {
var oDate = util.scand("d/m/yyyy", event.value);
if (oDate!=null) {
var sDD = formatOrdinalDay(oDate);
event.value = (sDD + util.printd(" of mmmm yyyy", oDate));
}
}
function formatOrdinalDay(d) {
if (d.getDate()==11 || d.getDate()==12 || d.getDate()==13) return d.getDate() + "th";
else if ((d.getDate()%10==1)) return d.getDate() + "st";
else if ((d.getDate()%10==2)) return d.getDate() + "nd";
else if ((d.getDate()%10==3)) return d.getDate() + "rd";
else return d.getDate() + "th";
}
Copy link to clipboard
Copied
Sorry - I copied the wrong version. For today's date this is the script I am using:
var oDate = new Date();var sDay = util.printd("d", oDate);var aSuffix = ["", "st", "nd", "rd", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th", "th", "st"];var sDD = sDay + aSuffix[+sDay];event.value = (sDD + util.printd(" of mmmm yyyy", oDate));
Copy link to clipboard
Copied
Sorry, wrong post.
Copy link to clipboard
Copied
You need to better describe how it should work. So the user should enter "4/2/2022" and it should format it to "4th of February 2022"?
Copy link to clipboard
Copied
The fillable field formatting is set to the date category. It's set to custom - dd of mmmm yyyy
The form user would just select the date from the calendar that shows up within the fillable field.
Then I have a "Run custom validation script" set up with the script that I proivded in the original question - to try to add the "th" "nd" "st" "rd"
Copy link to clipboard
Copied
The validation script is used to check what the user types. It doesn't affect what the user sees. You probably want a formatting script - check the original instructions.
Copy link to clipboard
Copied
Technically that's true, but many times the Validation script is used to change the value entered by the user. It's handy because it doesn't execute each time any field's value is changed (like the calculation script), only when that specific field is edited.
Copy link to clipboard
Copied
Try this code as the field's custom Validation script:
if (event.value) {
var oDate = util.scand("d/m/yyyy", event.value);
if (oDate!=null) {
var sDD = formatOrdinalDay(oDate);
event.value = (sDD + util.printd(" of mmmm yyyy", oDate));
}
}
function formatOrdinalDay(d) {
if (d.getDate()==11 || d.getDate()==12 || d.getDate()==13) return d.getDate() + "th";
else if ((d.getDate()%10==1)) return d.getDate() + "st";
else if ((d.getDate()%10==2)) return d.getDate() + "nd";
else if ((d.getDate()%10==3)) return d.getDate() + "rd";
else return d.getDate() + "th";
}
Copy link to clipboard
Copied
That's working perfectly. Thank you!
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more