Extract a date suffix with MomentJS to make it superscript

I don't think MomentJS has the function you're looking for; I'd say you should go to the MomentJS site and submit it as a feature suggestion. You never know, it might even get implemented.

In the meanwhile, an alternative option might be to use the configuration options in MomentJS to change the Ordinal strings (ie the suffixes) so that they include a <sup> tag.

See the relevant section in the MomentJS manual here: http://momentjs.com/docs/#/customization/ordinal/

You'd end up writing a function that looks like this:

moment.locale('en', {
    ordinal: num => {
        const b = num % 10;
        const output = (~~(num % 100 / 10) === 1) ? 'th' :
            (b === 1) ? 'st' :
                (b === 2) ? 'nd' :
                    (b === 3) ? 'rd' : 'th';
        return num + '<sup>' + output + '</sup>';
    },
});

The above example is lifted directly from MomentJS's manual, with just the return line being modified.


I looked at this for my own project, but decided to go with a regex solution.

myDate.format( 'Do MMMM YYYY' ).replace( /(\d)(st|nd|rd|th)/g, '$1<sup>$2</sup>' );

I appreciate the OP wanted to avoid regex, but this is a simple solution that may be beneficial to others.