NoFYE
NoFYE

Reputation: 11

Get month but needs to reset at the end of the year with javascript

I'm hoping you could help me with this. I am trying to create a script that will get month but needs to reset at the end of the year. The purpose is I want to let users know that a service will end in a month. What I have works fine until I get to the end of the year. I'm having a hard time thinking through the solution to calculate the end of the year, causing the script to reset to January. I hope articulated what I'm trying to do. Any help is appreciated. Here's what I have so far

var months = ["January", "February", "March", "April", 
"May","June","July","August","September","October","November","December"]
var d = new Date();
var mon = d.getMonth()+1;
var oneMonth = months[mon];

Upvotes: 0

Views: 309

Answers (3)

user2575725
user2575725

Reputation:

Use % to reset:

var mon = (d.getMonth()+1)%months.length;

var months = ["January", "February", "March", "April",
  "May", "June", "July", "August", "September", "October", "November", "December"
];
var dt = new Date('2018/11/20');
console.log(months[((dt.getMonth() + 1) % months.length)]);
console.log(months[((dt.getMonth() + 2) % months.length)]);
console.log(months[((dt.getMonth() + 3) % months.length)]);

OR, just use Date#setMonth()

var months = ["January", "February", "March", "April",
  "May", "June", "July", "August", "September", "October", "November", "December"
];
var dt = new Date('2018/12/31');
dt.setMonth(1 + dt.getMonth());
console.log(months[dt.getMonth()]);

dt.setMonth(2 + dt.getMonth());
console.log(months[dt.getMonth()]);

Upvotes: 1

ggorlen
ggorlen

Reputation: 57105

Use the modulus % operator to wrap your offset around the array. Modulus is a very important array tool in that you can add any offset and you'll remain in bounds of valid array indices 0-11.

In your case, (11 + 1) % 12 === 0 where 11 is December, 0 is January, 1 is the number of months you want to step into the future and 12 is the number of months in the year.

var months = ["January", "February", "March", "April", 
"May","June","July","August","September","October","November","December"];
var d = new Date();
var mon = d.getMonth() + 1;
var oneMonth = months[mon%months.length];
console.log(mon, oneMonth);

Upvotes: 1

OliverRadini
OliverRadini

Reputation: 6466

The other answers give you a way to do this with vanilla javascript; if you're interested in using a library, then date-fns isn't a bad choice, and makes it quite easy to work with dates.

const {
  parse,
  addMonths,
  getMonth,
} = dateFns;

const months = ["January", "February", "March", "April", 
"May","June","July","August","September","October","November","December"]

const newDate = getMonth(addMonths(
  parse(new Date()),
  1
))

const janDate = getMonth(addMonths(
  parse(new Date()),
  4
))

console.dir(months[newDate])
console.dir(months[janDate])
<script src="https://cdnjs.cloudflare.com/ajax/libs/date-fns/1.29.0/date_fns.min.js"></script>

Upvotes: 0

Related Questions