js实现两个日期的时间段,或距离当前时间的时间段,如:几秒前,几天前,几个月前

项目中需要显示在多久之前的更改记录,比如:3天前,2个月前。或者是工作的时间,比如:2010-06-01到2015-04-01是几年几个月。

下面是time.js文件的代码:

export default {
  //获取两个时间之间的时间段
  getTime: function(startTime, endTime) {
    let end
    if (endTime) {
      end = new Date(endTime)
    } else {
      end = new Date()
    }
    let endYear = end.getFullYear()
    let endMonth = end.getMonth() + 1
    let endDay = end.getDate()
    let endHour = end.getHours()
    let endMinute = end.getMinutes()
    let endSecond = end.getSeconds()

    let start = new Date(startTime)
      // 获取某一时间的年月日
    let startYear = start.getFullYear()
    let startMonth = start.getMonth() + 1
    let startDay = start.getDate()
    let startHour = start.getHours()
    let startMinute = start.getMinutes()
    let startSecond = start.getSeconds()

    let year, month, day, hour, minute, second

    //计算
    year = (endYear - startYear) > 0 ? (endYear - startYear) : 0

    if (endMonth > startMonth) {
      month = endMonth - startMonth
    } else if (endMonth == startMonth) {
      month = 0
    } else {
      if (year > 0) {
        year = year - 1
      }
      month = 12 - startMonth + endMonth
    }

    if (endDay > startDay) {
      day = endDay - startDay
    } else if (endDay == startDay) {
      day = 0
    } else {
      if (startMonth == 1 || startMonth == 3 || startMonth == 5 || startMonth == 7 || startMonth == 8 || startMonth == 10 || startMonth == 12) {
        day = 31 - startDay + endDay
      } else if (startMonth == 2) {
        //闰年 能被4整除 2月份是29天,不能就是28天
        if (startYear % 4 == 0 && startYear % 100 != 0 || startYear % 400 == 0) {
          day = 29 - startDay + endDay
        } else {
          day = 28 - startDay + endDay
        }
      } else {
        day = 30 - startDay + endDay
      }
      if (month > 0) {
        month = month - 1
      }
    }

    if (endHour > startHour) {
      hour = endHour - startHour
    } else if (endHour == startHour) {
      hour = 0
    } else {
      hour = 24 - startHour + endHour
      if (day > 0) {
        day = day - 1
      }
    }

    if (endMinute > startMinute) {
      minute = endMinute - startMinute
    } else if (endMinute == startMinute) {
      minute = 0
    } else {
      minute = 60 - startMinute + endMinute
      if (hour > 0) {
        hour = hour - 1
      }
    }

    if (endSecond > startSecond) {
      second = endSecond - startSecond
    } else if (endSecond == startSecond) {
      second = 0
    } else {
      second = 60 - startSecond + endSecond
      if (minute > 0) {
        minute = minute - 1
      }
    }

    return {
      year: year,
      month: month,
      day: day,
      hour: hour,
      minute: minute,
      second: second
    }
  }
}

调用方法:

import time from 'time.js';

time.getTime('2015-06-01 11:20:04', '2018-03-01 12:40:05');

返回结果:

{ //可根据需要显示月数或天数
    year: 3,
    month: 3,
    day: 0,
    hour: 1,
    minute: 20,
    second: 1
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值