The work of magic is based on the fact that if the number of days is transferred to the date constructor in more than the current month, the date is automatically spent next month.
I.e
new Date(2017, 7, 33)
Will not return on August 33 and the second of September.
getDate() will return the day ordinal of the month, respectively . That is, 2 - for the second of September, 1 - for the first.
Accordingly, transferring 32 to the constructor dates for the month with 30 days - we will come to the second number, for the month with 31 days - to the first. With 28 days - on the fourth. This number and subtract.
Accordingly, this magic is tied to the non-obvious (although described in the specification) property of the date constructor, and I would recommend such a generous comment.