2

Is there any way to get a localized format in DayJS plugin but without the day? I need to show in a calendar the month and year only but depending on the locale, the month can be on the first place or at the end.

I've checked in the docs (https://day.js.org/docs/en/display/format#localized-formats) but it seems there is nothing with year and month only.

dayjs().format("LL") // December 7, 2022
// what I would like to get: December 2022 or 2022 December (depending on the locale)

Thanks in advance

Proz1g
  • 1,177
  • 4
  • 16
  • 36

1 Answers1

1

I end up using a regex to delete the part I didn't want.

dayjs().format("LL").replace(/\b\s*\d{1,2}\s*\b/g, '');

DEMO:

["December 7, 2022", "7 Décembre 2022"].map(date => console.log(date.replaceAll(/\b\s*\d{1,2}\s*\b/g, '')));
  • Thank you, it's a nice way to solve it since I think there is no way to get what I wanted officially. – Proz1g Apr 13 '23 at 11:48
  • This assumes that all languages are of the same format which might not be true – yangli-io Jun 15 '23 at 01:25
  • Not necessarily, in my example I test 2 different languages which have 2 different formats. Obviously I didn't try all languages, but the regex is precise enough to fit most of the cases. The best solution would be dayjs supporting this. – Maxime Lechevallier Jun 21 '23 at 08:31