Reputation: 735
Code:
months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
];
currentMonth: String;
this.currentMonth = this.months[date.getMonth()];
nextMonth() {
this.currentMonth = ...
}
How do I go about building the nextMonth function to change the value of currentMonth? For example if its value is currently 'August', what should I add into my function to increment its value by 1, meaning, change its value to 'September'?
Upvotes: 0
Views: 40
Reputation: 147146
You can use indexOf
to look up currentMonth
and increment:
nextMonth = months[(months.indexOf(currentMonth) + 1) % 12];
It is probably better to simply store the month number in currentMonth
. Then you can simply increment currentMonth:
currentMonth = (currentMonth + 1) % 12;
and only reference the months array when you need the name:
currentMonthName = months[currentMonth];
Upvotes: 3