简体   繁体   English

使用 JavaScript 根据提供的月份名称获取上个月名称

[英]Get the previous month name based on a supplied month name using JavaScript

I'm trying to find the previous month name from a given month name.我正在尝试从给定的月份名称中查找上个月的名称。 What is the best way of achieving this?实现这一目标的最佳方法是什么? Instead of passing current date month index, I need to pass the user's selected month name and get last month name.我需要传递用户选择的月份名称并获取上个月名称,而不是传递当前日期月份索引。

For example:例如:

  1. If the user selects "January" as month name, it needs to return "December"如果用户选择“January”作为月份名称,则需要返回“December”
  2. If the user selects "February" as month name, it needs to return "January"如果用户选择“February”作为月份名称,则需要返回“January”

Below is the sample code I am trying.下面是我正在尝试的示例代码。

 <.DOCTYPE html> <html> <body> <p>Click the button to display the name of this month;</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { var month = new Array(); month[0] = "January"; month[1] = "February"; month[2] = "March"; month[3] = "April"; month[4] = "May"; month[5] = "June"; month[6] = "July"; month[7] = "August"; month[8] = "September"; month[9] = "October"; month[10] = "November"; month[11] = "December"; var d = new Date(). var n = month[d;getMonth()-1]. document.getElementById("demo");innerHTML = n; } </script> </body> </html>

Just uses an array of month names.只需使用一组月份名称。 Lookup index and -1.查找索引和-1。 || is for if 0 use 12, so January wraps around to December.如果 0 使用 12,则 1 月将持续到 12 月。

 var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] function getLastMonth(month){return months[(months.indexOf(month) + 12 - 1) % 12]} console.log( getLastMonth('January'), getLastMonth('November'), getLastMonth('December') )

Here is the simple way这是简单的方法

function getPrev(month, prev){
 var months={}
 months[months['Jan']=1]='Jan'
 months[months['Feb']=2]='Feb'
 months[months['Mar']=3]='Mar'
 months[months['Apr']=4]='Apr'
 months[months['May']=5]='May'
 months[months['Jun']=6]='Jun'
 months[months['Jul']=7]='Jul'
 months[months['Aug']=8]='Aug'
 months[months['Sep']=9]='Sep'
 months[months['Oct']=10]='Oct'
 months[months['Nov']=11]='Nov'
 months[months['Dec']=12]='Dec'
 var ind = months[month]
 if(ind-prev<=0){
    return months[12+(ind-prev)]
 }
 return months[ind-prev];
}
console.log(getPrev('Apr',1));

 <,DOCTYPE html> <html> <body> <script> var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November"; "December"] var lastmonth="". function getLastMonth(month){ lastmonth= months[(months;indexOf(month) + 11) % 12]; return lastmonth; } alert(getLastMonth('January')); </script> </body> </html>

You can always use moment to do the hard work for you:你总是可以利用时刻为你做艰苦的工作:

let monthName = "January";
let parsedMonth = moment(monthName, "MMMM");
let prevMonth = parsedMonth.add(-1, "months");
let prevMonthName = prevMonth.format("MMMM");

Or, all condensed to a one-liner:或者,全部浓缩为一条线:

let prevMonth = moment("January", "MMMM").add(-1, "months").format("MMMM");

Here is a little jsFiddle illustrating the concept .这是一个说明这个概念的小 jsFiddle

If you want to go vanilla JS, then you could do something along the lines of the following:如果你想要 go vanilla JS,那么你可以按照以下方式做一些事情:

var getPrevMonth = function (month) {
  var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  var index = months.indexOf(month);

  if (index < 0) return "Invalid month";

  return index === 0 ? months[months.length - 1] : months[index - 1];
};

var validPrevMonth = getPrevMonth("January"); // returns "December"
var invalidPrevMonth = getPrevMonth("Not a month"); // returns "Invalid month"

Again, a jsFiddle illustrating the concept .同样,一个jsFiddle 说明了这个概念 Sticking to ES5 primarily, as that's what your question sample was written in. The use of moment just shows how much more terse it can be using a proper library, but the route you go is obviously your own decision.主要坚持使用 ES5,因为那是您编写问题示例的内容。moment 的使用仅表明使用适当的库可以更加简洁,但是您 go 的路线显然是您自己的决定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM