DEV Community

loading...
Cover image for How to get month list in your locale

How to get month list in your locale

pretaporter profile image Maksim Updated on ・1 min read

Hi there! I am glad to show simple TypeScript function, that maybe will save your time. In many cases you need to implement selector of months. In some cases it should be in different locales. See it below:

function getMonthList(
  locales?: string | string[],
  format: "long" | "short" = "long"
): string[] {
  const year = new Date().getFullYear(); // 2020
  const monthList = [...Array(12).keys()]; // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
  const formatter = new Intl.DateTimeFormat(locales, {
    month: format
  });

  const getMonthName = (monthIndex: number) =>
    formatter.format(new Date(year, monthIndex));

  return monthList.map(getMonthName);
}

That's it. Just provide your locale as param.
For example, 🇬🇧 getMonthList('en') will return:

January
February
March
April
May
June
July
August
September
October
November
December

Try it by yourself on codesandbox.

For short version of names just provide second param as short.
For example, 🇬🇧 getMonthList('en', 'short') will return:

Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
Sep
Oct
Nov
Dec

Follow me in Twitter

Update
By your requests I have extracted function in package

Discussion (11)

pic
Editor guide
Collapse
umutakyol profile image
asdsadasf

Hi, great article, thanks.

Which part of this code is typescript, It's like all javascript to me, but some parts seems strange. Maybe those parts, lol?

Collapse
pretaporter profile image
Maksim Author

Thank you! JavaScript version:

function getMonthList(locales, format = "long") {
  const year = new Date().getFullYear(); // 2020
  const monthList = [...Array(12).keys()]; // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
  const formatter = new Intl.DateTimeFormat(locales, {
    month: format
  });

  const getMonthName = (monthIndex) =>
    formatter.format(new Date(year, monthIndex));

  return monthList.map(getMonthName);
}
Collapse
dannyengelman profile image
Danny Engelman • Edited

Using the 2nd parameter mapFn on Array.from instead of .map saves a spread on monthList

function getMonthList(locales, format = "long") {
  const year = new Date().getFullYear(); // 2020
  const monthList = Array(12).keys(); // an Array Iterator
  const formatter = new Intl.DateTimeFormat(locales, {
    month: format
  });
  const getMonthName = (monthIndex) =>  formatter.format(new Date(year, monthIndex));

  return Array.from(monthList , getMonthName);
}


`

Thread Thread
dannyengelman profile image
Danny Engelman • Edited

And since year is not a parameter, no need for const year declaration to get a monthname

new Date(0, monthIndex) will do, for ES Arrow function:

const getMonthList = (locale = "en", notation = "long" ) =>
  Array.from(
    Array(12).keys(),
    key => Intl.DateTimeFormat(locale, {
      month: {
         s : "short",
         n : "numeric"
      } [notation[0]] || "long"
    })
    .format(new Date(0, key))
  );


`

Thread Thread
pretaporter profile image
Maksim Author

Cool, thanks!

For constructor of Intl.DateTimeFormat locales is optional param. No requirement to provide default value of locale.

Collapse
lifelongthinker profile image
Sebastian

Please note that locale is a distinctly different concept than language. Because these work with defaulting mechanisms they may seem the same in your example, but they are not.

Collapse
pretaporter profile image
Maksim Author

Thanks, fixed

Collapse
mvalentine512 profile image
Mark Valentine

Sorry I know this is a bit of an old thread but I can't find an answer via google. Could someone please explain line 8 of Danny Engleman's 2nd post.

I get that it is creating an object as the 2nd parameter to "DateTimeFormat" with 1 attribute named "month" that maps to another object.

What does [notation[0]] || "long" do? To me it looks like it should be a 2nd attribute of the outer object, but in my mind it should have a comma.

Thank you in advance.

Collapse
mvalentine512 profile image
Mark Valentine

Never mind. I got it.

Collapse
pretaporter profile image
Maksim Author

Amazing work!