Actually, the snippet itself
Not so long ago I ran into a task that would allow to get the number of days in a specified month in JavaScript. Regular function for this in the language unfortunately not.
On this subject, one elegant mechanism was coined, using one well-known feature of many programming languages. If we set a non-existent date for a month (for example, April 31), then as a result our facility will store the corresponding date of the next month (in this case, May 1).
Thus, in order to get the number of days in a specified month, it is necessary to subtract the result of the above operation from the number 32. That is, if you set the date as April 32, as a result we get May 2. Check: 32-2 = 30 - the number of days will be in April.
')
var days_in_april = 32 - new Date(2013, 3, 32).getDate();
Mode of application
Using the excellent prototyping feature in JavaScript, you can extend the built-in Date object with your own method:
Date.prototype.daysInMonth = function() { return 32 - new Date(this.getFullYear(), this.getMonth(), 32).getDate(); };
Thus, in order to get the number of days in the current month, it will suffice to contact the Date directly:
alert(new Date().daysInMonth());
However, as it turned out over time in practice - this method is still not perfect. At least not for Safari.
Safari Features
Sometimes the results of this snippet did not coincide with the expected, and in Safari, instead of the usual 30 and 31, you could get a unit. The study of the problem showed that it turns out that the described feature of the language does not work in Safari 100% of the time when working with dates. This is best illustrated by the following example:
<script> var date = new Date(1982, 9, 32); document.write(date); </script>
And this is what we see in Chrome 26 and Safari 6, respectively:

Perfect practice
To get out and avoid cluttering up unnecessary checks specifically for Safari, it is enough to change the reference number 32 to 33. This method works in Safari as well as in other browsers:
Date.prototype.daysInMonth = function() { return 33 - new Date(this.getFullYear(), this.getMonth(), 33).getDate(); };