Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Our favorite bug for newbies using moment.js:

  const somedate = moment(...somedate...);
  const oneMonthLater = somedate.add(1, 'M');
  const dateToCheck = ...; // between somedate and somedate + 1 month

  if (dateToCheck.isBetween(somedate, oneMonthLater)) {
    // do something
  }
The puzzled looks when the tests fail. I'll miss that. Seriously.


Just to confirm that I remember Moment correctly, the code in the if statement never runs because .add mutates somedate, so somedate and oneMonthLater are the same, correct?


Yes, `somedate` is mutable.


Gotta love immutables after that...


Yes, it bit me once




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: