Search code examples
javascriptreactjsmomentjsdayjs

Does dayjs support ordinals in month? (Feb. instead of Feb, for germany language)


I am trying to get following representation of a month in German: Feb.

I tried so far:

'Do MMM YYYY'

and

dayjs(date).format('Do MMM YYYY')

With momentjs you used to get 1. Feb. 2012 but with dayjs I get 1. Feb 2012. I tried MMMo but didn't work.


Solution

  • There seems to be no format supporting a conditional '.' for cases such as the german "März". The cleanest workaround I could come up with is the following:

    const date = dayjs("2019-03-24").locale("de")
    date.format(`MMM${[2, 5, 6].includes(date.$M) ? '' : '.'} YYYY`)
    

    This checks if the provided date is a month with 4 character (März, Juni or Juli) and does not add the '.' in such cases. All other months will be shortened with a '.' at the end, e.g. "Jan.", "Feb." etc..