【问题标题】:How can I get previous month name using `switch`?如何使用`switch`获取上个月的名称?
【发布时间】:2020-10-15 11:53:53
【问题描述】:

var month_name;
switch(new Date().getMonth()) {
    case 0 :
        month_name = "January";
    break;
    case 1 :
        month_name = "February";
    break;
    case 2 :
        month_name = "March";
    break;
    case 3 :
        month_name = "April";
    break;
    case 4 :
        month_name = "May";
    break;
    case 5 :
        month_name = "June";
    break;
    case 6 :
        month_name = "July";
    break;
    case 7 :
        month_name = "October";
    break;
    case 8 :
        month_name = "September";
    break;
    case 9 :
        month_name = "October";
    break;
    case 10 :
        month_name = "November";
    break;
    case 11 :
        month_name = "December";
    break;
}
console.log(month_name - 1);

这段代码的结果是NaN

我正在尝试获取上个月的名称,但结果不是数字。

如何获取上个月的名称?

【问题讨论】:

  • 你为什么要添加-1
  • 我有一个日历工作,我试图得到昨天星期几的名称,我不知道该怎么做,只是在尝试
  • 例如今天是星期四,我想赶上星期三
  • @WebDev 并再次更新以制作功能

标签: javascript switch-statement


【解决方案1】:

switch 语句更改为:

 switch(new Date().getMonth() - 1) {

并将case -1 : 添加到 12 月。

var month_name;
switch(new Date().getMonth() - 1) {
    case 0 :
        month_name = "January";
    break;
    case 1 :
        month_name = "February";
    break;
    case 2 :
        month_name = "March";
    break;
    case 3 :
        month_name = "April";
    break;
    case 4 :
        month_name = "May";
    break;
    case 5 :
        month_name = "June";
    break;
    case 6 :
        month_name = "July";
    break;
    case 7 :
        month_name = "October";
    break;
    case 8 :
        month_name = "September";
    break;
    case 9 :
        month_name = "October";
    break;
    case 10 :
        month_name = "November";
    break;
    case 11 :
    case -1 :
        month_name = "December";
    break;
}
console.log(month_name);

【讨论】:

  • 不,这不是我正在搜索的内容,我不想更改 switch 语句,因为我需要当前月份的名称,下一个和上一个
  • 然后将case 12: 添加到一月。 getMonth() 返回一个介于 0 和 11 之间的数字,因此一个月的任一方给出的范围是 -1 到 +12。如果你想要比这更多的月份,你将需要模运算,或者使用@mplungjan 的第一种方法,这种方法更标准。
【解决方案2】:

不清楚为什么需要开关

要获取月份的名称,您可以这样做

const months = ["January", "Febtruary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "Dececember"];
const getMonthName = offset => { 
  let d = new Date();
  d.setHours(15, 0, 0, 0); // normalise
  return months[d.getMonth()+offset];
};  
const prevMonth = getMonthName(-1);
const thisMonth = getMonthName(0);
const nextMonth =getMonthName(+1)
console.log("\n",
"Last month was", prevMonth,"\n",
"This month is", thisMonth,"\n",
"Next month will be", nextMonth
)

或者使用intl

const getMonth = offset => { 
  let d = new Date(); 
  d.setFullYear(d.getFullYear(),d.getMonth()+offset,15,15, 0, 0, 0); // normalise to mid month at 3pm
  return new Intl.DateTimeFormat('en-US', { month: 'long' }).format(d)
};

const lastMonth = getMonth(-1);
const thisMonth = getMonth(0);
const nextMonth = getMonth(+1);

console.log("\n",
"Previous month was",lastMonth,"\n",
"This month is",thisMonth,"\n",
"Next month will be",nextMonth,"\n",
)

为了昨天

const getDayName = offset => {
    let d = new Date();
    d.setHours(15, 0, 0, 0); // normalise
    d.setDate(d.getDate() + offset);
    return new Intl.DateTimeFormat('en-US', {weekday: 'long' }).format(d);
 };

console.log("\n",
"Yesterday was", getDayName(-1),"\n",
"Today is", getDayName(0),"\n",
"Tomorrow will be", getDayName(1),"\n"
)

如果你坚持切换。先减去月份:

const getMonthName = offset => {
  let d = new Date(), month_name;
  d.setMonth(d.getMonth()+offset); // this will wrap to 12 from January
  switch (d.getMonth()) {
    case 0:  return "January";
    case 1:  return "February";
    case 2:  return "March";  
    case 3:  return "April";  
    case 4:  return "May";    
    case 5:  return "June";   
    case 6:  return "July";   
    case 7:  return "October";
    case 8:  return "September";
    case 9:  return "October";
    case 10: return "November"; 
    case 11: return "December"; 
  }
}
console.log("\n",
"Last month",getMonthName(-1),"\n",
"This month",getMonthName(0),"\n",
"Next month",getMonthName(1),"\n"
)

【讨论】:

    猜你喜欢
    • 2018-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-03
    • 1970-01-01
    相关资源
    最近更新 更多