How can I format today as "June 26th" rather than "June 26"?

I am trying to create a macro for filling in dates and times for use with Roam Research. Roam chooses to format dates like June 26th, 2020 rather than June 26 2020 and this gives me a problem as I cannot seem to figure out how get the ICU formatting functions to add the appropriate 'st' 'nd' 'rd' or 'th' to day numbers.

I've combed the wiki and ICU site but come up empty.

Can anyone help me?

Thanks.

Matt

1 Like

@mrpasini has you covered: "Fancy" Ordinal Date Formatting

4 Likes

While someone has already covered this ground I had built a solution using JS before I saw the reply:

Here's mine for reference, it seems to work okay:

    Retrospective
Triggered by any of the following:
The exact case string “;;retro” is typed (then deleted)
Will execute the following actions:
Execute JavaScript For Automation
'use strict';
(function run() {
    var ordinal_suffix_of = function( i ) {
        var j = i % 10,
        k = i % 100;
        if( j == 1 && k != 11 ) {
            return "st";
        }
        if( j == 2 && k != 12 ) {
            return "nd";
        }
        if( j == 3 && k != 13 ) {
            return "rd";
        }
        return "th";
    };

    var form_date = function( d ) {
        var day = d.getDate();
        var month = d.toLocaleString( 'default', { month: 'long' } );
        var year = d.getFullYear();
        return month + " " + day + ordinal_suffix_of( day ) + ", " + year;
    };

    var kme = Application("Keyboard Maestro Engine");

    var today = new Date();

    var last_week = new Date( today.getFullYear(), today.getMonth(), today.getDate() - 7 );
    kme.setvariable( "last_week", { to: form_date( last_week ) } );

    var last_month = new Date( today.getFullYear(), today.getMonth() - 1, today.getDate() );
    kme.setvariable( "last_month", { to: form_date( last_month ) } );

    var last_quarter = new Date( today.getFullYear(), today.getMonth() - 3, today.getDate() );
    kme.setvariable( "last_quarter", { to: form_date( last_quarter ) } );

    var last_year = new Date( today.getFullYear() - 1, today.getMonth(), today.getDate() );
    kme.setvariable( "last_year", { to: form_date( last_year ) } );
})();
Stop macro and notify on failure.
Insert Text by Pasting
Last Week: [[%Variable%last_week%]]%LineFeed%
Last Month: [[%Variable%last_month%]]%LineFeed%
Three Months Ago:[[%Variable%last_quarter%]]%LineFeed%
One Year Ago:[[%Variable%last_year%]]%LineFeed%
2 Likes

This gist has a really clever ordinal converter that taught me a few things about JavaScript. I wrote an excessively detailed explanation of it.

3 Likes