Q: How does the daily script calculate the number of days in a specified month?
A: In this case ....
1: function daysInMonth(iMonth, iYear)
2: {
3: return 32 - new Date(iYear, iMonth, 32).getDate();
4: }
As for why, you may understand it after careful consideration, but his ideas are indeed clever and he can't help but give me a sigh of relief.
New Date (iyear, Imonth, 32), in Javascript, if the parameter overflows, that is, when the number of days exceeds the month, it is automatically converted to the next month, for example, new (2009,0, 32), where 32 exceeds the 31 days of the 1949th month, and the date obtained at this time is 2009.1.1 (index starts from 0, 1 indicates January 1, 2009.1 ). New (2009,1, 32), 2009.2 month only has 28 days, then the date obtained is 2009.3.4.
Getdate () is the "day" in the returned date ". The number of days is the overflow part, and 32-date is used to get the number of days that can be accommodated by the specified month.
At the same time, I think of a piece of news that I saw that day: "How to count 25 of the 30 apples as quickly as possible" (which probably means this). This news is about kindergarten children, there are numbers 1, 2, 3, 4, 5, 6 ...... Jing, including reporters, thought this was very interesting. In fact, they were so small that they were not very familiar with the log and could understand the concept of "complementing, today, this problem also reflects the same problem.
This idea is not only effective in Javascript, but also effective in calculating the number of days in the month .....