0

I am trying to create an errand reminder. The idea is the user can create a reminder, and they can set the frequency of the reminder. They can choose to be reminded daily, weekly or monthly. Now if they choose weekly, I only need to know the day of week they want to be reminded and all is well. The issue however is monthly. What if the user wants to be reminded every 31st of the month? Some months obviously don't have that, and being that the reminder goes out every month, I have no reason to keep track of the month itself.

I am really not sure how to about this. If it matters, this is a node.js app.

Chaim Friedman
  • 6,124
  • 4
  • 33
  • 61
  • 2
    it is not worth much but moment.js is a great library, anytime you deal with dates. – Kevin Amiranoff Jan 25 '17 at 18:41
  • If they want to be reminded on the 31st of every month then your user is broken. You cannot fix users outside of preventing silly, non-sensical decisions, like this one. – Dave Newton Jan 25 '17 at 18:41
  • Don't forget February, which has 28, and occasionally 29 days! Obviously, you'll need to figure something out, and what you do is a matter of opinion. Some people will advocate for using "the last day of the month" for those kinds of things. – Heretic Monkey Jan 25 '17 at 18:41
  • consider approaching the problem as "days from the beginning/end of the month" instead of "day of month" – zzzzBov Jan 25 '17 at 18:41
  • to say that the user is broken isnt really true. Take for instance a user that wants to be reminded of a monthly appointment, or bill. Is it not possible that theses will be scheduled on the 31st? – Chaim Friedman Jan 25 '17 at 18:43
  • Yes, it is not possible it will be scheduled on the 31st of every month, because not every month has 31 days. It may be scheduled on the *last* day of the month, or on the 1st, but not on the 31st. You cannot schedule things for days that do not exist. – Dave Newton Jan 25 '17 at 18:47
  • you make a good point. I think this is what I will do. – Chaim Friedman Jan 25 '17 at 18:48
  • There are [*many, many*](http://stackoverflow.com/search?q=%5Bjavascript%5D+last+day+of+month) duplicates. – RobG Jan 26 '17 at 06:38

1 Answers1

3

If a month does not have a 31st (or 30th like Februrary) you could use the last day of the month as fallback.

JavaScript gives you the last day of a month, when you create a date with the following month and set the day-component to zero:

new Date(yyyy, mm, dd)

whereby: - mm denotes the month, starting at 0 - dd denotes the day, starting at 1

new Date(2017, 0, 25) is January 25th 2017

new Date(2017, 1, 0) is January 31st 2017

Bruno
  • 141
  • 9