代码:
months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
];
currentMonth: String;
this.currentMonth = this.months[date.getMonth()];
nextMonth() {
this.currentMonth = ...
}如何构建nextMonth函数来更改currentMonth的值?例如,如果它的值当前是‘8月’,我应该在我的函数中添加什么来将它的值递增1,也就是说,将它的值改为‘9月’?
发布于 2018-08-19 07:09:18
您可以使用indexOf查找currentMonth并递增:
nextMonth = months[(months.indexOf(currentMonth) + 1) % 12];简单地将月份数字存储在currentMonth中可能会更好。然后,您可以简单地递增currentMonth:
currentMonth = (currentMonth + 1) % 12;并且仅在需要名称时引用months数组:
currentMonthName = months[currentMonth];https://stackoverflow.com/questions/51913168
复制相似问题