あなたのロケールで月リストを得る方法


こんにちは!私は、多分あなたの時間を節約する簡単なtypescript機能を示すことがうれしいです.多くの場合、数ヶ月のセレクタを実装する必要があります.場合によっては、それは異なるロケールにあるべきです.以下を参照ください.
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);
}
それです.ちょっとあなたのロケールをparamとして提供してください.
例えば、🇬🇧 getMonthList('en')は戻ります.
January
February
March
April
May
June
July
August
September
October
November
December
codesandboxで自分で試してみてください.
名前の短いバージョンのために、ちょうどshortとして第2のPAAMを提供します.
例えば、🇬🇧 getMonthList('en', 'short')は戻ります.
Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
Sep
Oct
Nov
Dec
フォローミー
更新
あなたの要求によって私はpackageで機能を抽出しました