Jump to content

Convert Date format to read out in this format.


jalberts1

Recommended Posts

Between FusionPro's DateFromString and FormatDate functions, and generic methods of the JavaScript Date object, you can get the English name of the month (i.e. May) and the four-digit year, for a given date. For instance:

var date = DateFromString("5/18/2018");
return "the " + date.getDate() + " day of " + FormatDate(date, "MMM") + " in the year " + date.getFullYear();

 

The part you're missing is how to spell out the day of the month, not as "18", and also not as "eighteen", but as "eighteenth", which is called an ordinal number.

 

That's really a generic JavaScript question (i.e. it's not specific to FusionPro), so your best bet is to search on the Internet, since hundreds of thousands of people are using JavaScript and have already thought of almost every common usage case like this. A Google search for "JavaScript spell out ordinal numbers" returns a lot of hits, including many from Stack Overflow, which is a Q&A site for programming questions. I found one with an answer that seems to work here:

https://stackoverflow.com/questions/20425771/how-to-replace-1-with-first-2-with-second-3-with-third-etc

 

So, combining the code in that answer with what we already had above:

var special = ['zeroth','first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh', 'eighth', 'ninth', 'tenth', 'eleventh', 'twelfth', 'thirteenth', 'fourteenth', 'fifteenth', 'sixteenth', 'seventeenth', 'eighteenth', 'nineteenth'];
var deca = ['twent', 'thirt', 'fort', 'fift', 'sixt', 'sevent', 'eight', 'ninet'];

function stringifyNumber(n) {
 if (n < 20) return special[n];
 if (n%10 === 0) return deca[Math.floor(n/10)-2] + 'ieth';
 return deca[Math.floor(n/10)-2] + 'y-' + special[n%10];
}

var date = DateFromString("5/18/2018");
return "the " + stringifyNumber(date.getDate()) + " day of " + FormatDate(date, "MMM") + " in the year " + date.getFullYear();

You could also put everything but the last two lines into the JavaScript Globals if you want to use it in multiple rules.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...