Search code examples
node.jsdate-fns

Date-fns: how to get exact date output as moment ? e.g. put "th" after the day


This is my test node.js code for comparison, node 16.16.0, "moment-timezone": "0.5.43", "date-fns": "2.30.0",

const moment = require("moment-timezone");
const { intlFormat } = require('date-fns'); 

let d0 = moment().format("dddd, MMMM Do YYYY, h:mm:ss a");
console.log(d0);

let d1 = intlFormat(new Date(), { dateStyle: 'full', timeStyle: 'medium' }, { locale: 'en-US', })
console.log(d1);

The output i get is

Monday, December 11th 2023, 3:55:27 pm
Monday, December 11, 2023 at 3:55:27 PM

So how to add "th" after 11 ? how to dislay pm not PM ? Thanks !


Solution

  • let d2 = format(d, "EEEE, MMMM do yyyy, h:mm:ss a");
    console.log(d2);
    

    The complete format is here https://date-fns.org/v2.16.1/docs/format Took me a while to find it, wish there's an easier way to find it for beginner.