PDA

View Full Version : Including Ordinal when Formatting Date


-Lisa-
March 28th, 2016, 08:03 AM
Can the FormatDate function be used to include ordinals (i.e. "th", "st", etc.)?

Dan Korn
March 28th, 2016, 10:04 AM
No, but if you search the web for "JavaScript ordinals", you'll find lots of functions to do that, such as this one (http://stackoverflow.com/a/31615643/3207828):
function getOrdinal(n)
{
var s = ["th","st","nd","rd"];
var v = n % 100;
return s[(v-20)%10] || s[v] || s[0];
}
You can use it in a rule like this:
var theDate = Today();
var dayOfMonth = theDate.getDate();
var dayWithOrdinal = getOrdinal(dayOfMonth);
return FormatDate(theDate, "EE, MMM d") + dayWithOrdinal + FormatDate(theDate, ", yyyy");

Adding this functionality to FormatDate wouldn't be a bad enhancement idea for FusionPro, although such ordinals are obviously specific to English, and not all of our users, and certainly not all output files, are English-based. Different languages do ordinals much differently:
https://en.wikipedia.org/wiki/Ordinal_indicator

-Lisa-
March 28th, 2016, 01:04 PM
AWESOME! Thank you, Dan! This was super helpful!!!!