使用本地化的JS获取月份名称

34

可以使用本地的实现获取本地化的完整月份名称。

var objDate = new Date("10/11/2009"),
    locale = "en-us",
    month = objDate.toLocaleString(locale, { month: "long" });

但是这只能得到给定日期的月份数字,我想要得到对应于月份数字的月份名称。例如,如果我执行getMonth(2),它将返回February。如何使用本机(不使用像moment这样的库)实现getMonth


1
这是一个一行解决方案:new Date(2020, monthIndex, 1).toLocaleString("en-us", {month: "long"}); - Bitterblue
3个回答

63

你已经接近成功:

var getMonth = function(idx) {

  var objDate = new Date();
  objDate.setDate(1);
  objDate.setMonth(idx-1);

  var locale = "en-us",
      month = objDate.toLocaleString(locale, { month: "long" });

    return month;
}

console.log(getMonth(1));
console.log(getMonth(12));


你知道是否有一种方法可以不需要实例化日期对象来完成吗? - user779159
1
我不会完全依赖编程语言:只需在本地化文件中添加一个月份名称数组(如果没有,则创建一个)。 - k102
7
我知道这是一个很久以前的答案,但这种情况在2月的29、30或31日以及其他月份会导致Date对象无法正常工作,因为该对象是用当前日期初始化的。要解决这个问题,可以在设置月份之前添加objDate.setDate(1); - Vince0789

1

要获取一年中的所有月份和一周中的天数,需要循环遍历一组日期,并使用适当的选项使用 toLocaleString 来获取所需值:

function getLocalDayNames() {
  let d = new Date(2000,0,3); // Monday
  let days = [];
  for (let i=0; i<7; i++) {
    days.push(d.toLocaleString('default',{weekday:'long'}));
    d.setDate(d.getDate() + 1);
  }
  return days;
}

console.log(getLocalDayNames());

function getLocalMonthNames() {
  let d = new Date(2000,0); // January
  let months = [];
  for (let i=0; i<12; i++) {
    months.push(d.toLocaleString('default',{month:'long'}));
    d.setMonth(i + 1);
  }
  return months;
}

console.log(getLocalMonthNames());

语言 default 意味着 toLocaleString 使用代码运行环境的默认语言。


1

您可以使用本地化格式:

const DAYS_IN_WEEK = 7;
const MONTHS_IN_YEAR = 12;

function localizedWeekdayNames(
  locale: Intl.LocalesArgument = "default",
  dateStyle: Intl.RelativeTimeFormatStyle = "short"
): string[] {
  const dayNames: string[] = [];
  const currentDate = new Date();
  while (currentDate.getDay() !== 0) {
    currentDate.setDate(currentDate.getDate() + 1);
  }
  for (let day = 0; day < DAYS_IN_WEEK; day++) {
    dayNames.push(
      currentDate.toLocaleDateString(locale, { weekday: dateStyle })
    );
    currentDate.setDate(currentDate.getDate() + 1);
  }
  return dayNames;
}

function localizedMonthNames(
  locale: Intl.LocalesArgument = "default",
  dateStyle: Intl.RelativeTimeFormatStyle = "short"
): string[] {
  const monthNames: string[] = [];
  const currentDate = new Date();
  while (currentDate.getMonth() !== 0) {
    currentDate.setMonth(currentDate.getMonth() + 1);
  }
  for (let month = 0; month < MONTHS_IN_YEAR; month++) {
    monthNames.push(
      currentDate.toLocaleDateString(locale, { month: dateStyle })
    );
    currentDate.setMonth(currentDate.getMonth() + 1);
  }
  return monthNames;
}

// ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"] 
console.log(localizedWeekdayNames());

// ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] 
console.log(localizedMonthNames());

这里是生成的JS代码,带有一些额外的本地化输出。

const DAYS_IN_WEEK = 7;
const MONTHS_IN_YEAR = 12;

function localizedWeekdayNames(locale = "default", dateStyle = "short") {
  const dayNames = [];
  const currentDate = new Date();
  while (currentDate.getDay() !== 0) {
    currentDate.setDate(currentDate.getDate() + 1);
  }
  for (let day = 0; day < DAYS_IN_WEEK; day++) {
    dayNames.push(currentDate.toLocaleDateString(locale, {
      weekday: dateStyle
    }));
    currentDate.setDate(currentDate.getDate() + 1);
  }
  return dayNames;
}

function localizedMonthNames(locale = "default", dateStyle = "short") {
  const monthNames = [];
  const currentDate = new Date();
  while (currentDate.getMonth() !== 0) {
    currentDate.setMonth(currentDate.getMonth() + 1);
  }
  for (let month = 0; month < MONTHS_IN_YEAR; month++) {
    monthNames.push(currentDate.toLocaleDateString(locale, {
      month: dateStyle
    }));
    currentDate.setMonth(currentDate.getMonth() + 1);
  }
  return monthNames;
}

// Sun Mon Tue Wed Thu Fri Sat
console.log(...localizedWeekdayNames());

// Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
console.log(...localizedMonthNames());

// domingo lunes martes miércoles jueves viernes sábado
console.log(...localizedWeekdayNames('es-ES', 'long'));

// enero febrero marzo abril mayo junio
// julio agosto septiembre octubre noviembre diciembre
console.log(...localizedMonthNames('es-ES', 'long'));


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接