Skip to main content
Inspiring
February 4, 2022
Answered

Date with th rd st or nd Help

  • February 4, 2022
  • 3 replies
  • 3195 views

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?

This topic has been closed for replies.
Correct answer try67

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

3 replies

try67
Community Expert
Community Expert
February 5, 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"?

Inspiring
February 7, 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.

 

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"

Legend
February 7, 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.

JR Boulay
Community Expert
Community Expert
February 4, 2022

Sorry, wrong post.

Acrobate du PDF, InDesigner et Photoshopographe
Inspiring
February 4, 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));