[Answer silly girl] How does one calculate the number of days in a specified month in the script?

Source: Internet
Author: User

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 .....

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.