<html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>Title page</title> <script language="javascript"> function getLastDay(year,month) { var new_year = year; //Get the current year var new_month = month++; //Get the first day of the month for easy calculation (the last day is not fixed) if(month>12) //If the current year is greater than December, then the year will be changed to next year { new_month -=12; //Decrease the month by new_year++; //Increase the year} var newnew_date = new Date(new_year,new_month,1); //Get the first day of the current month return (new Date(new_date.getTime()-1000 *60*60*24)).getDate();//Get the last day of the month} </script> <body> <input id="Button1" type="button" value="Take the last day of May 2007" onClick="alert(getLastDay(2007,5))" /> </body> </html>js gets the maximum number of days in a month
There is a wonderful thing about the date construction method new Date("xxxx/xx/xx") in JS.
When you pass in "xxxx/xx/0" (number 0), the date obtained is the last day of the month before month "xx" (the maximum value of month "xx" is 69, off topic),
When you pass in "xxxx/xx/1" (No. 1), the date obtained is the first day of the month after the month "xx" (understand by yourself)
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", you will get the last day of February of xxxx year. It will automatically determine whether the current year is a leap year and return 28 or 29. You don't have to judge by yourself.
Therefore, if we want to get how many days there are in the selected year and month, we only need
var temp=new Date("Select year/select month+1/0");
return temp.getDate()//Maximum number of days
For verification, you can also use this method.
The following is the getDaysInMonth(year, month) method written in JS to get the number of days in a certain year and month:
Copy the code code as follows:
function getDaysInMonth(year,month){
month = parseInt(month,10)+1;
var temp = new Date(year+"/"+month+"/0");
return temp.getDate();
}
The method to get the number of days in a certain month using javascript is as follows:
Goal: Get the number of days in 2014/April
//Construct a date object: var day = new Date(2014,4,0); //Get the number of days: var daycount = day.getDate();
Explanation: OK, the number of days you want has been released. getDate() is the last day obtained, which is the number of days in the month. Note: When we construct the date object, 4 is actually the actually constructed month of May, because the month starts from 0. As for the third number of 0 days, the minimum requirement is the 1st. If it is smaller than the 1st, it becomes the last day of March you want.