js计算两个时间相差的年、月、日、时、分、秒。

           

 /* 时间格式:2016-8-31 10:35:00 或 2016/8/31 10:35:00 */

var dateObj=dayCha('2016-8-17','2019-6-30') //执行方法,参数为时间,第二个时间未填默认为当前时间。

方法一:

function dayCha(time, twoTime) {
  time = time.replace(new RegExp("-", "gm"), "/");
  if (twoTime) {
    twoTime = twoTime.replace(new RegExp("-", "gm"), "/");
  }
  else {
    twoTime = new Date();
  }
  // 计算比较日期
  const getMaxMinDate = (time, twoTime, type) => {
    let minTime = new Date(time).getTime() - new Date(twoTime).getTime() > 0 ? twoTime : time
    let maxTime = new Date(time).getTime() - new Date(twoTime).getTime() > 0 ? time : twoTime
    let maxDateDay = new Date(new Date(maxTime).getFullYear(), new Date(maxTime).getMonth() + 1, 0).getDate()
    let maxMinDate = new Date(minTime).getDate() > maxDateDay ? maxDateDay : new Date(minTime).getDate()
    let maxMinTong = null
    if (type == 'month') {
      maxMinTong = new Date(maxTime).getFullYear() + '/' + (new Date(minTime).getMonth() + 1) + '/' + maxMinDate + ' ' + new Date(minTime).toLocaleTimeString('chinese',{hour12:false})
    } else {
      maxMinTong = new Date(maxTime).getFullYear() + '/' + (new Date(maxTime).getMonth() + 1) + '/' + maxMinDate + ' ' + new Date(minTime).toLocaleTimeString('chinese',{hour12:false})
    }
    return {
      minTime,
      maxTime,
      maxMinTong
    }
  }
  // 相差年份
  const getYear = (time, twoTime) => {
    let oneYear = new Date(time).getFullYear()
    let twoYear = new Date(twoTime).getFullYear()
    // 计算出与最小日期的日时分相同的最大日期的值
    // 如2016-8-17和2019-6-30,计算出来的maxMinTong为2019-8-17
    const { minTime, maxTime, maxMinTong } = getMaxMinDate(time, twoTime, 'month')
    // 计算相差多少年
    let chaYear = Math.abs(oneYear - twoYear)
    // 如果计算出的maxMinTong(如2019-8-17)大于了最大日期,则表示最后一年没有满,需要减一
    if (new Date(maxMinTong).getTime() > new Date(maxTime).getTime()) {
      chaYear--
    }
    return chaYear
  }
  const getMonthTong = (time, twoTime) => {
    let oneMonth = new Date(time).getFullYear() * 12 + (new Date(time).getMonth() + 1)
    let twoMonth = new Date(twoTime).getFullYear() * 12 + (new Date(twoTime).getMonth() + 1)
    return Math.abs(oneMonth - twoMonth)
  }
  // 相差月份
  const getMonth = (time, twoTime, value) => {
    // 计算出与最小日期的日时分相同的最大日期的值
    // 如2016-8-17和2019-6-30,计算出来的maxMinTong为2019-6-17
    const { minTime, maxTime, maxMinTong } = getMaxMinDate(time, twoTime, 'day')
    // 计算最小日期到maxMinTong(如2019-6-17)的月份,这样刚好对应月日
    let chaMonth = getMonthTong(minTime, maxMinTong)
    // 如果计算出来的maxMinTong(如2019-6-17)大于最大日期,则表示最后一月没有满,需要减一
    if (new Date(maxMinTong).getTime() > new Date(maxTime).getTime()) {
      chaMonth--
    }
    if (value) {
      return chaMonth - value
    } else {
      return chaMonth
    }
  }
  // 相差天数
  const getDay = (time, twoTime, value) => {
    let chaTime = Math.abs(new Date(time).getTime() - new Date(twoTime).getTime());
    if (value) {
      return parseInt(chaTime / 86400000) - value
    } else {
      return parseInt(chaTime / 86400000)
    }
  }
  // 相差小时
  const getHour = (time, twoTime, value) => {
    let chaTime = Math.abs(new Date(time).getTime() - new Date(twoTime).getTime());
    if (value) {
      return parseInt(chaTime / 3600000) - value
    } else {
      return parseInt(chaTime / 3600000)
    }
  }
  // 相差分钟
  const getMinute = (time, twoTime, value) => {
    let chaTime = Math.abs(new Date(time).getTime() - new Date(twoTime).getTime());
    if (value) {
      return parseInt(chaTime / 60000) - value
    } else {
      return parseInt(chaTime / 60000)
    }
  }
  // 相差秒
  const getSecond = (time, twoTime, value) => {
    let chaTime = Math.abs(new Date(time).getTime() - new Date(twoTime).getTime());
    if (value) {
      return parseInt(chaTime / 1000) - value
    } else {
      return parseInt(chaTime / 1000)
    }
  }
  // 相差年月日时分秒
  const getDffeYMDHMS = (time, twoTime) => {
    // 计算出与最小日期的日时分相同的最大日期的值
    // 如2016-8-17和2019-6-30,计算出来的maxMinTong为2019-6-17
    const { minTime, maxTime, maxMinTong } = getMaxMinDate(time, twoTime, 'day')
    // 计算最小日期到(如:2019-6-17)的天数,这样算出来的天数刚好数足月的
    let dffeDay1 = getDay(minTime, maxMinTong)
    // 判断一下,如果计算出来的(如:2019-6-17)大于最后的日期(如2019-6-30)就需要减去(2019-6-30到2019-6-17的天数,否则不需要)
    if (new Date(maxMinTong).getTime() > new Date(maxTime).getTime()) {
      let prevMonth = new Date(maxMinTong).getMonth() - 1
      let lastTime=new Date(maxMinTong).setMonth(prevMonth)
      dffeDay1 = dffeDay1 - getDay((new Date(lastTime).getFullYear() + '/' + (new Date(lastTime).getMonth()+1) + '/' + new Date(lastTime).getDate()), maxMinTong)
    }
    let dffeYear = getYear(time, twoTime)
    let dffeMonth = getMonth(time, twoTime, dffeYear * 12)
    let dffeDay = getDay(time, twoTime, dffeDay1)
    let dffeHour = getHour(time, twoTime, getDay(time, twoTime) * 24)
    let dffeMinute = getMinute(time, twoTime, (getDay(time, twoTime) * 24 * 60 + dffeHour * 60))
    let dffeSecond = getSecond(time, twoTime, (getDay(time, twoTime) * 24 * 60 * 60 + dffeHour * 60 * 60 + dffeMinute * 60))
    console.log(`年:${dffeYear},月:${dffeMonth},日:${dffeDay},时:${dffeHour},分:${dffeMinute},秒:${dffeSecond}`)
    return { dffeYear, dffeMonth, dffeDay, dffeHour, dffeMinute, dffeSecond }
  }
  // 相差年月周日时分秒
  const getDffeYMWDHMS = (time, twoTime) => {
    // 计算出与最小日期的日时分相同的最大日期的值
    const { minTime, maxTime, maxMinTong } = getMaxMinDate(time, twoTime, 'day')
    let dffeDay1 = getDay(minTime, maxMinTong)
    // 如果大于了,就需要减掉多余天数
    if (new Date(maxMinTong).getTime() > new Date(maxTime).getTime()) {
      let prevMonth = new Date(maxMinTong).getMonth() - 1
      let lastTime=new Date(maxMinTong).setMonth(prevMonth)
      dffeDay1 = dffeDay1 - getDay((new Date(lastTime).getFullYear() + '/' + (new Date(lastTime).getMonth()+1) + '/' + new Date(lastTime).getDate()), maxMinTong)
    }
    let dffeYear = getYear(time, twoTime)
    let dffeMonth = getMonth(time, twoTime, dffeYear * 12)
    let dffeDayZong = getDay(time, twoTime, dffeDay1)
    let dffeWeek = parseInt(dffeDayZong / 7)
    let dffeDay = dffeDayZong - dffeWeek * 7
    let dffeHour = getHour(time, twoTime, getDay(time, twoTime) * 24)
    let dffeMinute = getMinute(time, twoTime, (getDay(time, twoTime) * 24 * 60 + dffeHour * 60))
    let dffeSecond = getSecond(time, twoTime, (getDay(time, twoTime) * 24 * 60 * 60 + dffeHour * 60 * 60 + dffeMinute * 60))
    console.log(`年:${dffeYear},月:${dffeMonth},周:${dffeWeek},日:${dffeDay},时:${dffeHour},分:${dffeMinute},秒:${dffeSecond}`)
    return { dffeYear, dffeMonth, dffeWeek, dffeDay, dffeHour, dffeMinute, dffeSecond }
  }
 
  let chaYear = getYear(time, twoTime)
  let chaMonth = getMonth(time, twoTime)
  let chaDay = getDay(time, twoTime)
  let chaHour = getHour(time, twoTime)
  let chaMinute = getMinute(time, twoTime)
  let chaSecond = getSecond(time, twoTime)
  return {
    chaYear, chaMonth, chaDay, chaHour, chaMinute, chaSecond,
    getDffeYMDHMS: getDffeYMDHMS(time, twoTime),
    getDffeYMWDHMS: getDffeYMWDHMS(time, twoTime)
  }
 
}

具体详细解说

方法二:

function dayCha(startime,endtime){
 startime = startime.replace(new RegExp("-","gm"),"/");//转换-变为/
 endtime = endtime.replace(new RegExp("-","gm"),"/");
 var minTime = new Date(startime).getTime()>new Date(endtime).getTime()?endtime:startime
 var maxTime = new Date(startime).getTime()>new Date(endtime).getTime()?startime:endtime

 var start       = new Date(minTime);
 var startYear   = start.getFullYear(); //开始年份
 var startMonth  = start.getMonth() + 1;//开始月份


 var end      = new Date(maxTime);
 var endYear  = end.getFullYear(); //结束年份
 var endMonth = end.getMonth() + 1;//结束月份

//  年
 var mideleYear=new Date(minTime)
 mideleYear.setFullYear(endYear)
 var years = Math.abs(mideleYear.getFullYear() - start.getFullYear());
 if(mideleYear.getTime()>end.getTime()){
  years--
 }

// 月
var mideleMonthDay = new Date(minTime)
mideleMonthDay.setFullYear(endYear)
mideleMonthDay.setMonth(endMonth-1)
console.log(mideleMonthDay.toLocaleDateString())
var months = (endYear*12+endMonth) - (startYear*12+startMonth) - years*12
if(mideleMonthDay.getTime()>end.getTime()){
  months--
}

// 日
var day = (Math.floor((end.getTime() - start.getTime()) / 24 / 60/ 60 / 1000))
if(end.getTime()> mideleMonthDay.getTime()){
  day = (Math.floor( (end.getTime() - mideleMonthDay.getTime()) / 24 / 60/ 60 / 1000)) - day
}else{
  let prevMonthDay = new Date(mideleMonthDay)
  prevMonthDay.setMonth(endMonth-2)
  day = (Math.floor((end.getTime() - prevMonthDay.getTime()) / 24 / 60/ 60 / 1000))
}

var chaTime = end.getTime() - start.getTime()

var chaDay = Math.floor(chaTime / 24 / 60/ 60 / 1000)

var hour = Math.floor(chaTime / 60/ 60 / 1000) - chaDay*24

var min = Math.floor(chaTime / 60 / 1000) - chaDay*24*60 - hour*60

var sec =  Math.floor(chaTime / 1000) - chaDay*24*60*60 - hour*60*60 - min*60

var chastr = years+'年/'+months+'月/'+day+'天 '+hour+'时:'+min+'分:'+sec+'秒';

console.log('相差时间:'+chastr);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值