轻量级的JavaScript时间库(Moment.js)

Moment.js是一个轻量级的JavaScript时间库,方便对时间的操作,提高开发效率

安装:
	npm install moment
引入:
	import moment from 'moment'; (或者var moment = require('moment');)
使用:
	(以常用的几个时间为例:)
fastTime(type){
      console.log(type)
      if(type == 'yesterday'){//昨日
        const yestStart =  moment().subtract(1, "days").format('YYYY-MM-DD 00:00:00')
        const yestEnd = moment().subtract(1, "days").format('YYYY-MM-DD 23:59:59')
        this.searchTime = [yestStart,yestEnd]
      }else if(type == 'today'){//今日
        const TODAYSTART = moment().startOf("day").format("YYYY-MM-DD 00:00:00");
        const TODAYEND = moment().endOf("day").format("YYYY-MM-DD 23:59:59");
        this.searchTime = [TODAYSTART,TODAYEND]
      }else if(type == 'lastweek'){//上周
        const startLastWeek = moment().day(-6).startOf("day").format("YYYY-MM-DD HH:mm:ss");
        const endLastWeek = moment().day(0).endOf("day").format("YYYY-MM-DD HH:mm:ss");
        this.searchTime = [startLastWeek,endLastWeek]
      }else if(type == 'week'){//本周
        const startWeek = moment().day(1).startOf("day").format("YYYY-MM-DD HH:mm:ss");
        const endWeek = moment().day(7).endOf("day").format("YYYY-MM-DD HH:mm:ss");
        this.searchTime = [startWeek,endWeek]
      }else if(type == 'lastmonth'){//上月
        const startLastMon = moment().month(moment().month() - 1).startOf("month").valueOf() ;
        const endLastMon = moment().month(moment().month() - 1).endOf("month").valueOf();
        const a = moment(startLastMon).format("YYYY-MM-DD HH:mm:ss")
        const b = moment(endLastMon).format("YYYY-MM-DD HH:mm:ss")
        this.searchTime = [a,b]
      }else if(type == 'month'){//本月
        const startMon = moment().month(moment().month()).startOf("month").valueOf();;
        const endMon = moment().month(moment().month()).endOf("month").valueOf();;
        const c = moment(startMon).format("YYYY-MM-DD HH:mm:ss")
        const d = moment(endMon).format("YYYY-MM-DD HH:mm:ss")
        this.searchTime = [c,d]
      }
      console.log(this.searchTime,type,"时间")
    },

详细内容请查看moment.js官网
moment.js官网

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值