Vue时间管理大师

VUE时间管理大师


前言

这篇帖子主要是整理了我查找过的方法,很多时候记不住原贴了,以后新加的我会把原贴附上。


时间格式转换

getTime (time) {
		// 在这个位置如果new Date()参数为空是获取当前时间
    	// 也可以传个时间来转个格式
    	// 1、new Date("month dd,yyyy hh:mm:ss"); 
		// 2、new Date("month dd,yyyy"); 
		// 3、new Date(yyyy,mth,dd,hh,mm,ss); 注意:这种方式下,必须传递整型;
		// 4、new Date(yyyy,mth,dd); 
		// 5、new Date(ms); 注意:ms:是需要创建的时间和 GMT时间1970年1月1日之间相差的毫秒数;当前时间与GMT1970.1.1之间的毫秒数:var mills = new Date().getTime();
		// date是标准时间格式:Fri Jun 02 2017 12:00:00 GMT+0800 (中国标准时间)    酱紫
        let date = ''
        time ? (date = new Date(time)) : (date = new Date())
        let year = date.getFullYear()
        let month = date.getMonth() + 1
        let day = date.getDate()
        let hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
        let minute = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
        let second = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
        month >= 1 && month <= 9 ? (month = '0' + month) : ''
        day >= 0 && day <= 9 ? (day = '0' + day) : ''
        let timer = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second
        return timer
      }

参考于 NEW DATE()之参数传递

比较两个日期的大小

// date1 => date2 返回 true
comparedate(date1, date2) {
      const oDate1 = new Date(date1)
      const oDate2 = new Date(date2)
      if (oDate1.getTime() === oDate2.getTime()) {
        return true
      } else if (oDate1.getTime() > oDate2.getTime()) {
        return true
      } else {
        return false
      }
    },

转载于 日期大小比较

获得一个天数的前后几天

// num为正整数时往前推, num为负整数时往后推
    getBeforeDate(num, time) {
      let n = num
      let d = ''
      if (time) {
        d = new Date(time)
      } else {
        d = new Date()
      }
      let year = d.getFullYear()
      let mon = d.getMonth() + 1
      let day = d.getDate()
      if (day <= n) {
        if (mon > 1) {
          mon = mon - 1
        } else {
          year = year - 1
          mon = 12
        }
      }
      d.setDate(d.getDate() - n)
      year = d.getFullYear()
      mon = d.getMonth() + 1
      day = d.getDate()
      const s = year + '-' + (mon < 10 ? ('0' + mon) : mon) + '-' + (day < 10 ? ('0' + day) : day)
      return s
    },

获得某一天在当年的周数

getWeeks(date) {
      // str格式为yyy-mm-dd
      // 周日归到了本周
      var d = new Date(date)
      var originDate = d.getFullYear() + '-' + '01' + '-' + '01' + ' 00:00:00'
      var nowDate = d.getFullYear() + '-' + ((d.getMonth() + 1) > 9 ? (d.getMonth() + 1) : '0' + (d.getMonth() + 1)) + '-' + (d.getDate() > 9 ? d.getDate() : '0' + d.getDate()) + ' 00:00:00'
      var nowStrapTime = (new Date(nowDate)).getTime()
      var originStrapTime = (new Date(originDate)).getTime()
      var originDay = ((new Date(originStrapTime)).getDay()) === 0 ? 7 : ((new Date(originStrapTime)).getDay())
      var nowDay = d.getDay() === 0 ? 7 : d.getDay()
      var weeks = 0
      var resultStrap = 0
      if (nowDay > originDay) {
        resultStrap = nowStrapTime - (nowDay - originDay) * 1000 * 3600 * 24 - originStrapTime
        if (resultStrap > 0) {
          weeks = (resultStrap / (3600 * 1000 * 24 * 7)) + 1
        } else {
          weeks = 1
        }
      } else {
        resultStrap = nowStrapTime + (originDay - nowDay) * 1000 * 3600 * 24 - originStrapTime
        weeks = (resultStrap / (3600 * 1000 * 24 * 7)) + 1
      }
      // 获得周数
      return weeks
    },

JS原生的关于时间方法

date.getTime() // 时间转时间戳 13位的 精确到毫秒的 如果需要十位的除以1000
date.getDate() // 日 1 到 31
date.getMonth() // 月 0 到 11
date.getFullYear() // 年 yyyy
date.getHours() // 时 0 到 23
date.getMinutes() // 分 0 到 59
date.getSeconds() // 秒 0 到 59
date.getMilliseconds() // 毫秒 0 到 999
date.getDay() // 获得星期 返回值是 0(周日) 到 6(周六) 之间的一个整数

总结

不定期更新,过段时间再整理个关于list的

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值