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

Date with th rd st or nd Help

Explorer ,
Feb 04, 2022 Feb 04, 2022

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?

TOPICS
Create PDFs , Edit and convert PDFs , General troubleshooting , How to , JavaScript , PDF forms
3.3K
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 ,
Feb 07, 2022 Feb 07, 2022

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";	
}

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 ,
Feb 04, 2022 Feb 04, 2022

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

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 ,
Feb 04, 2022 Feb 04, 2022

Sorry, wrong post.


Acrobate du PDF, InDesigner et Photoshopographe
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 ,
Feb 05, 2022 Feb 05, 2022

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

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 07, 2022 Feb 07, 2022

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.

defaultrfjue3aoi30b_0-1644251998574.png

 

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"

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
LEGEND ,
Feb 07, 2022 Feb 07, 2022

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.

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 ,
Feb 07, 2022 Feb 07, 2022

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.

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 ,
Feb 07, 2022 Feb 07, 2022

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";	
}
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 08, 2022 Feb 08, 2022
LATEST

That's working perfectly. Thank you!

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