JS get the last day of the month and JS to get the maximum number of days a month

Source: Internet
Author: User
Tags getdate

Original link: http://www.jb51.net/article/44468.htm

First, get the last day of the month (possibly 28/29/30/21) problem, the idea is simply to get the first day of the next month, the number of milliseconds from 1970, and then subtract the number of milliseconds from the day, which is the number of milliseconds to 1970 years from the last day of the month, based on this number of milliseconds. The code is as follows:


Two, J s get one months maximum days

JS inside of the new date ("Xxxx/xx/xx") the date of the construction method has a beauty,

When you pass in the "xxxx/xx/0" (number No. 0), the date is the "XX" month the first one months of the last day ("XX" month's maximum value is 69, digression),

When you pass in the "XXXX/XX/1" (number 1th), the date is "XX" month after the first day of one months (oneself understand)

If you pass in "1999/13/0", you will get "1998/12/31". And the biggest advantage is that when you pass in "xxxx/3/0", will get XXXX year February of the last day, it will automatically determine whether it is a leap year to return 28 or 29, do not judge yourself,

So, we want to choose how many days the choice of year, only need

var temp=new Date ("Select year/Select Month +1/0");

return temp.getdate ()//Maximum days

Check, you can also use this method.

The following is a getdaysinmonth (year, Month) method written using JS to obtain the number of days of a certain month:

function Getdaysinmonth (year,month) {
       month = parseint (month,10) +1;
       var temp = new Date (year+ "/" +month+ "/0");
       return Temp.getdate ();

Use JavaScript to get the number of days for a month as follows:

Goal: Get 2014/April days

1 2 3 4 Constructs a Date object: var day = new Date (2014,4,0); Get days: var daycount = Day.getdate ();

Description: OK, the number of days you want has come out. GetDate () is the last day of acquisition, the number of days of the month Note: when we construct a Date object, 4 is actually constructed in May, because the month starts at 0. The third number of 0 days, the minimum requirement is 1th, smaller than 1th, it will be the last day of your March.

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.