Be aware of hidden Javascript features that can break your code before you say “ugh..”.
Consider the below code that prints today’s date -
|
|
All looks good? Let us now create a target date for my payment - it is 1.5 months from now.
|
|
Don’t mind the day gap, we are just living in the wrong time zone. We are happy with 15-Jun allright.
Wait, there is another way to set dates. We will try that for the heck of it.
|
|
We have just added a month to my target payment date. There is a God?
Not so great news if you are on the business side of things. And, the not-so-funny thing - I did not notice this until it “failed” in production.
Why did this happen? It turns out there is a legit reason.
When you input a dt2.setMonth(5)
-
- Month will be set to ‘June’
- Date will be defaulted to today’s date (='31’) since there was no date provided (as yet)
- Since there is no ‘31-Jun’, Javascript helpfully pushes date by one day -> ‘1-Jul’
Next up: we provide the actual date (‘15’), Javascript sets date to ‘15’.
End result - 15-Jul-2019.
Hmm.. it is time for a break. Where can I find the nearest ☢ 💥?