0

I'm working on a project in which I need to find what days are holidays, and display specific messages for those holidays.

With some holidays, like Christmas, it is simply the 25th of December. But for other holidays, such as thanksgiving, it is a little tricky in that it is the 4th Thursday in November.

The approach I'm using is splitting up which week.

day = moment()
month = day.month() + 1
weeknum = Math.floor((day - 1) / 7) + 1
holiday2 = month + "/" + weeknum + "/" + day

Today happens to be columbus day, and so based off of what I have, it should be '10/2/1'. 10th month, 2nd week, 1st day. (10/12). The problem that I am getting, is with what I have right now for holiday2, I am getting:

10/206381035675/1444667249719

So...I know technically I'm close in that I see the '10/2/1' but I am getting very large abnormal numbers.

My question is 2 fold. How would I condense the numbers so that instead of getting a 10 digit response, I would get just the 1 or two digits that I need?

Would there be a better way to approach this?

James Brierley
  • 4,630
  • 1
  • 20
  • 39
kdweber89
  • 1,984
  • 2
  • 19
  • 29

1 Answers1

3

moment() returns a date object. You need to get the date similar to how you are already getting the month. You can do this using .date().

day = moment();
month = day.month() + 1;
date = day.date();
weeknum = Math.floor((date - 1) / 7) + 1;
holiday2 = month + "/" + weeknum + "/" + date;

Your current solution doesn't work because of the way dates are converted to integers. Specifically, they are stored as the number of milliseconds since 1970/01/01.

This answer suggests the best way for getting the week number:

var weeknum = Math.ceil(date / 7);
Community
  • 1
  • 1
James Brierley
  • 4,630
  • 1
  • 20
  • 39