Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

Adobe Summit 2023 [19th to 23rd March, Las Vegas and Virtual] | Complete AEM Session & Lab list
SOLVED

How to format an ordinal date pattern

Avatar

Level 1

Hello,

I am trying to find a way to add formatting to a date field which will show the day as ordinal (1st, 2nd, 3rd, etc.).  I found another thread where someone asked a similar question and this was part of the response:

 

"This isn't exactly what you are after as it wont give you the ordinal (st, nd, th). If you want that you have to write you own formatting (or use one of the JavaScript date libraries, like MomentJS)"

 

Nothing else was asked about it in the thread so I don't know if the OP ever got a solution for it.  I know very little about JavaScript so would need an explanation if that is how to do it.  Any help on how to solve for this would be greatly appreciated.  Thanks

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

@TK0215 

You may refer to various solutions available to implement the use case with javascript.

One of the simple solutions:

 

function dateOrdinal(d) {
    return d+(31==d||21==d||1==d?"st":22==d||2==d?"nd":23==d||3==d?"rd":"th")
};

 

 

1 Reply

Avatar

Correct answer by
Employee Advisor

@TK0215 

You may refer to various solutions available to implement the use case with javascript.

One of the simple solutions:

 

function dateOrdinal(d) {
    return d+(31==d||21==d||1==d?"st":22==d||2==d?"nd":23==d||3==d?"rd":"th")
};