Getting nominative month name in date-fns
Asked Answered
D

3

18

How can I get the name of the month in nominative format?

import { format } from 'date-fns'; 
import { ru } from 'date-fns/locale';

format(new Date(), 'MMMM', { locale: ru }); // июня

How can I get the name like юинь instead of июня?

Deathtrap answered 4/7, 2020 at 14:24 Comment(0)
D
33

I was able to find a solution. To get the stand-alone name of the month the LLLL format should be used.

The working code should look like this:

import { format } from 'date-fns'; 
import { ru } from 'date-fns/locale';

format(new Date(), 'LLLL', { locale: ru }); // июнь

See documentation -> Month (stand-alone): https://date-fns.org/v2.14.0/docs/format

Deathtrap answered 5/7, 2020 at 13:6 Comment(0)
H
17

There are a few patterns you can use.

  • LLL: Jan, Feb, ..., Dec
  • LLLL: January, February, ..., December
  • LLLLL: J, F, ..., D
  • MMM: Jan, Feb, ..., Dec
  • MMMM: January, February, ..., December
  • MMMMM: J, F, ..., D
Hartfield answered 15/2, 2022 at 12:33 Comment(2)
Any difference between LLL and MMM for example? Or are they just aliases for the same thing?Seurat
@Seurat in the default locale (en-US) they are same, but in some locales they differ a little bit. In other languages LLL is stand-alone unit, MMM is not which means LLL returns the name of a month, when MMM is declined according to the rules of the language.Hartfield
S
2

Use the LLLL format to get full month names. I have tried and it worked! example:


import {format} from 'date-fns';
const formattedDate = format(new Date(), 'LLLL');
Stinkwood answered 31/5, 2022 at 19:29 Comment(2)
When answering questions on Stack Overflow, never put code examples in links, and especially never in image links. Use the Code Sample option in the text window to automatically format your code sample. See how to format code manually.Vesical
Links are OK, but for images I mostly agree. Also @StephenMIrving feel free to improve his response yourself.Clareta

© 2022 - 2025 — McMap. All rights reserved.