js获取时间 new Date

获取当前时间

      let newtimeData = new Date()
      let Y = newtimeData.getFullYear()//getFullYear方法以四位数字返回年份
      let M = newtimeData.getMonth() + 1// getMonth方法从 Date 对象返回月份 (0 ~ 11),返回结果需要手动加一
      let D = newtimeData.getDate()// getDate方法从 Date 对象返回一个月中的某一天 (1 ~ 31)
      let h = newtimeData.getHours()// getHours方法返回 Date 对象的小时 (0 ~ 23)
      let m = newtimeData.getMinutes()// getMinutes方法返回 Date 对象的分钟 (0 ~ 59)
      let s = newtimeData.getSeconds()// getSeconds方法返回 Date 对象的秒数 (0 ~ 59)
      this.timeData = Y+'-'+ M +'-' + D
      // console.log('当前时间',this.timeData)

获取前一天的时间

// 开始前一个月时间
      let lastMonthTimeR = new Date(new Date().getTime() - 1 * 24 * 60 * 60 * 1000)
      let lastMonthY = lastMonthTimeR.getFullYear()
      let lastMonthM = lastMonthTimeR.getMonth() + 1
      let lastMonthD = lastMonthTimeR.getDate()
      let lastMonthh = lastMonthTimeR.getHours()
      let lastMonthm = lastMonthTimeR.getMinutes()
      let lastMonths = lastMonthTimeR.getSeconds()
      let lastMonthTime = lastMonthY +'-'+ lastMonthM+'-' + lastMonthD +'-'+ lastMonthh +'-'+lastMonthm +'-'+lastMonths 

获取前一个月的时间

// 开始前一个月时间
      let lastMonthTimeR = new Date(new Date().getTime() - 30 * 24 * 60 * 60 * 1000)
      let lastMonthY = lastMonthTimeR.getFullYear()
      let lastMonthM = lastMonthTimeR.getMonth() + 1
      let lastMonthD = lastMonthTimeR.getDate()
      this.notCoveredListData.startTime = lastMonthY+'-'+ lastMonthM +'-' + lastMonthD
//时分秒同上

获取当天0点的时间戳

let timer =  new Date(new Date().toLocaleDateString()).getTime()

获取当天最晚时间的时间戳

new Date(new Date(new Date().toLocaleDateString()).getTime()+24*60*60*1000-1).getTime()

获取一个月最晚时间的时间戳

new Date(new Date(new Date().toLocaleDateString()).getTime()+30*24*60*60*1000-1).getTime()

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值