Home > Web Front-end > JS Tutorial > body text

Example code for JS to get the last day of the month and JS to get the maximum number of days in a month_javascript skills

WBOY
Release: 2016-05-16 17:08:56
Original
1242 people have browsed it
<html xmlns="http://www.w3.org/1999/xhtml" >   
<head>   
<title>标题页</title>   
<script language="javascript">   
function getLastDay(year,month)   
{   
 var new_year = year;  //取当前的年份   
 var new_month = month++;//取下一个月的第一天,方便计算(最后一天不固定)   
 if(month>12)      //如果当前大于12月,则年份转到下一年   
 {   
 new_month -=12;    //月份减   
 new_year++;      //年份增   
 }   
 var newnew_date = new Date(new_year,new_month,1);        //取当年当月中的第一天   
 return (new Date(new_date.getTime()-1000*60*60*24)).getDate();//获取当月最后一天日期   
}   
</script>   
<body>   
  <input id="Button1" type="button" value="取2007年5月的最后一天" onClick="alert(getLastDay(2007,5))" />   
</body>   
</html>  
Copy after login

js gets the maximum number of days in a month

The new Date("xxxx/xx/xx") date construction method in JS has a wonderful point,

When you pass in "xxxx/xx/0" (0th), the date obtained is the last day of the month before the "xx" month (the maximum value of the "xx" month is 69, question foreign languages),

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 in 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,

So, if we want to get how many days there are in the selected year and month, we only need to

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 code The code is as follows:

function getDaysInMonth(year,month){
month = parseInt(month,10) 1;
var temp = new Date(year "/" month "/0");
Return temp.getDate();
}

The method to use javascript to get the number of days in a certain month is as follows:

Goal: Get the number of days in April 2014

//构造一个日期对象:
var day = new Date(2014,4,0); 
//获取天数:
var daycount = day.getDate();
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template