计算当月天数

方法一

            let dayarr = [];
            let date = new Date();
            this.year = date.getYear();
            this.month = date.getMonth()+1;
//           当月第一天星期几
            let firstDay = (new Date(this.year,this.month-1,1)).getDay();

//            当月天数&个月天数,需判断本月与下月是否为同一年、
            let theYear = this.month == 12 ? this.year+1 : this.year;//判断下个月的年份
            let theMonth = this.month == 12 ? 1:this.month;//判断下个月的月份
//            下个月的第一天-1就是本月的最后一天,即本月的天数
            let themonthdays =  new Date(new Date(theYear,theMonth,1)-1).getDate();//本月天数
            let lastmonthdays = new Date(new Date(this.year,this.month-1,1)-1).getDate();//上月天数


方法二

JS: 如何计算一个月有多少天

复制代码
 1 function getCountDays() {
 2         var curDate = new Date();
 3         /* 获取当前月份 */
 4         var curMonth = curDate.getMonth();
 5        /*  生成实际的月份: 由于curMonth会比实际月份小1, 故需加1 */
 6        curDate.setMonth(curMonth + 1);
 7        /* 将日期设置为0, 这里为什么要这样设置, 我不知道原因, 这是从网上学来的 */
 8        curDate.setDate(0);
 9        /* 返回当月的天数 */
10        return curDate.getDate();
11 }
12 例如,  获取当前月份(现在是3月)的总天数: 
13 getCountDays()       // 返回31
复制代码
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值