moment.js formatting date wrong

I'm trying to get upcoming date using moment.js. The date is obtained, but it's formatted wrongly. According to docs, format('l') formats the date in dd/mm/yyyy format and format('L') formats date in mm/dd/yyyy. but I'm getting the same output for both. Here is my code.

let next = moment().add(30, 'days').format('l'); // Output 10/16/2019
let next = moment().add(30, 'days').format('L'); // Output 10/16/2019
let next = moment().add(30, 'days').format('DD/MM/YYYY'); // Works fine

I tried i with moment.js v2.24.0 what's wrong in here? am I missing something or doing it wrong way?

2 answers

  • answered 2019-09-15 19:02 T.J. Crowder

    According to docs, format('l') formats the date in dd/mm/yyyy format and format('L') formats date in mm/dd/yyyy.

    No, that isn't what the documentation says:

    Localized formats

    Because preferred formatting differs based on locale, there are a few tokens that can be used to format a moment based on its locale.

    There are upper and lower case variations on the same formats. The lowercase version is intended to be the shortened version of its uppercase counterpart.

    Month numeral, day of month, year:   L    09/04/1986
                                         l    9/4/1986
    

    There's nothing there saying L will use one field order and l another.

    It's using your current Moment locale to format the date. See the i18n section for how to set locales, globally or per instance.

  • answered 2019-09-15 19:02 KAUSHIK JANI

    According to docs

    moment().format('L'); // 09/16/2019 moment().format('l'); // 9/16/2019

    https://momentjs.com/