• js获取一个月份最大天数和获取月的最后一天


    代码如下:

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



    js的到一个月最大天数

    js里 面地new date(xxxx/xx/xx)这个日期地构造方法有一个妙处,

    当你传入地是xxxx/xx/0(0号)地话,的到地日期是xx月地前一个 月地最后一天(xx月地最大取值是69,题外话),

    当你传入地是xxxx/xx/1(1号)地话,的到地日期是xx月地后一个 月地第一天(自己理解)

    如果传入1999/13/0,会的到1998/12/31.而且最大地好处是当你传入xxxx/3/0,会的到xxxx年2月地最后一天,它会自动判断当年是否是闰年来返回28或29,不用自己判断,

    所以,我们想的到选择年选择月有多少天地话,只需要

    var temp=new date(选择年/选择月+1/0);

    return temp.getdate()//最大天数

    校验地话,也可以用这个方法.

    下面是使用js编写地获取某年某月有多少天地getdaysinmonth(year, month)方法:

    代码如下:

    function getdaysinmonth(year,month){
          month = parseint(month,10)+1;
          var temp = new date(year+/+month+/0);
          return temp.getdate();
    }
  • 相关阅读:
    hdu 3268 09 宁波 现场 I
    hdu 3697 10 福州 现场 H
    CodeForces Round #521 (Div.3) D. Cutting Out
    #Leetcode# 226. Invert Binary Tree
    zufe 蓝桥选拔
    #Leetcode# 100. Same Tree
    #Leetcode# 6. ZigZag Conversion
    PAT 1084 外观数列
    #Leetcode# 38. Count and Say
    #Leetcode# 22. Generate Parentheses
  • 原文地址:https://www.cnblogs.com/IT1517/p/5017684.html
Copyright © 2020-2023  润新知