JavaScript calculates the number of days in a specified month

Source: Internet
Author: User

When using JS to draw a working calendar, you need to use JS to calculate the total number of days of the specified month

On the Internet to find some methods, are more cumbersome, and later brainwave, think of a lazy way to share

First, the principle analysis

To get the number of days in a month, just take the date to the last day of the month.

Around this idea, flexible call setmonth (), GetMonth (), SetDate (), GetDate (), calculate the required date

Second, the Code implementation

function Getmonthlength (date) {  new  Date (date)  //  set Date to next month  D.setmonth (D.getmonth () +1)  d.setdate ('1')   // get the last day  of the Month D.setdate (D.getdate ()-1)  return  d.getdate ()}

Three, a little pain in the face

In less than an hour, he was beaten to the face by a man.

There are more simple ways to do it:

New 0). GetDate ()

When you create a time object using new Date (), if date is passed in 0, you can get the date from the last day directly through GetDate ()

JavaScript calculates the number of days in a specified month

Related Article

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.