获取每个月的第一天和最后一天

需求:获取某月的第一天或者最后一天,这里给出的函数是日月年通用

handlerTime(val, type) {
         // 当月---当天 默认当天,  非当月--月的最后一天   年 当天, 年最后一天 前7日当天 总 当天
         let infoDate = '';
         const format =
            type == 'day' ? 'YYYY-MM-DD' : type == 'month' ? 'YYYY-MM' : 'YYYY';
         const curtDate = moment().format(format);
         const defaultDate = moment(val).format(format);

         if(type == 'day') {
            infoDate = moment(val).format('YYYY-MM-DD');
         }
         else if(type == 'month') {
            infoDate =
               defaultDate == curtDate
                  ? moment().format('YYYY-MM') + '-01'
                  : moment(val).endOf('months').format('YYYY-MM-DD');
         }
         else {
            infoDate =
               defaultDate == curtDate
                  ? moment().format('YYYY-MM') + '-01'
                  : moment(val).endOf('year').format('YYYY-MM-DD');
         }

         return infoDate;
},


//日的掉用
this.handlerTime(moment(new Date()).format('YYYY-MM-DD'), 'day');

//月的掉用
this.handlerTime(moment(new Date()).format('YYYY-MM'), 'month');

//年的掉用
this.handlerTime(moment(new Date()).format('YYYY'), 'year');
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值