79403699

Date: 2025-01-31 18:30:28
Score: 1.5
Natty:
Report link

Never use setDate, setMonth and setYear in this way to instantiate a date. If the current month has less than 31 days, and you try to set a date on the 31st of a month, when you set the day, it will roll over to the 1st of the next month. Conversely if you were to invert the order of the calls, if you create your date on the 31st of a month, for a month with less than 31 days, when you set the month, it will roll over to the next month. Use the constructor, or at the least use setFullYear(year, month, day).

Reasons:
  • Long answer (-0.5):
  • No code block (0.5):
  • Single line (0.5):
  • Low reputation (1):
Posted by: Frederic Daigle