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

2,664 阅读8分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第12天,点击查看活动详情

使用

第一需要定义方法

  • 方法传递两个时间参数,如果只传入一个,就是和当前时间比较(参数格式:2022-10-1 12:00:00 || 2022/10/1 12:00:00, 以及:2022-10-1 || 2022/10/1)
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) => {}

  // 相差年份
  const getYear = (time, twoTime) => {}

  // 相差月份
  const getMonth = (time, twoTime, value) => {}

  // 相差天数
  const getDay = (time, twoTime, value) => {}

  // 相差小时
  const getHour = (time, twoTime, value) => {}

  // 相差分钟
  const getMinute = (time, twoTime, value) => {}

  // 相差秒
  const getSecond = (time, twoTime, value) => {}

  // 相差年月日时分秒
  const getDffeYMDHMS = (time, twoTime) => {}

  // 获取对应值并返回
  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)
  let chaDffeYMDHMS = getDffeYMDHMS(time, twoTime)
  return {
    chaYear, chaMonth, chaDay, chaHour, chaMinute, chaSecond,chaDffeYMDHMS
  }
}

第二内部方法一一说明

  • 计算比较日期

getMaxMinDate()是用于计算比较日期的时间

  1. 参数是比较的两个时间,以及type(month:计算相差年需要的,day:计算相差月需要的),使用2016-8-172019-6-15举例:

  1. 在计算相差多少年的时候:(type:month)

如2016-8-17和2019-6-15计算相差时间,通过getMaxMinDate()计算出来的maxMinTong:2019-8-17,minTime:2016-8-17,maxTime:2019-6-15

然后理论上2016-8-17(对比的开始时间)到2019-8-17是足足的3年,但是2019-8-17比2019-6-15(对比的结束时间)大,所以对比的两个时间其实没有满3年,需要减一年

2. 在计算相差多少月的时候:(type:day)

如2016-8-17和2019-6-15计算相差时间,通过getMaxMinDate()计算出来的maxMinTong:2019-6-17,minTime:2016-8-17,maxTime:2019-6-15

然后我们通过计算(201912+6)-(201612+8)=32,得出2016-8-17(对比的开始时间)到2019-6-17足足相差32月,但是2019-6-17比2019-6-15(对比的结束时间)大了,因此没有满足32月,需要减去一月

3. 计算去除年月相差天数时需要减去的足年月天数:(type:day)

如2016-8-17和2019-6-15计算相差时间,通过getMaxMinDate()计算出来的maxMinTong:2019-6-17,minTime:2016-8-17,maxTime:2019-6-15

然后通过 let dffeDay1 = getDay('2016/8/17','2019/6/17')计算出相差的天数,但是由于2019-6-17比2019-6-15(对比的结束时间)大了,所以不能减去当前算出来的dffeDay1天,需要dffeDay1 = dffeDay1 - getDay(2019/5/17, '2019/6/17')(需要减去最大日期前一个月的相同日期到maxMinTong(2019-6-17),即减去一个月的天数).最后得到dffeDay1为足年月(相差年+相差月)的天数

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
    }
  }
  • 相差年份

getYear()是用于计算相差年份

计算maxMinTong具体详情

1.time:比较的第一时间

2.twoTime:比较的第二个时间

 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
  }

  • 相差月份

getMonth()是用于计算相差月份

计算maxMinTong具体详情

1.time:比较的第一时间

2.twoTime:比较的第二个时间

3.value:需要减去的月份(基本上是减去足年的月份)

 const getMonth = (time, twoTime, value) => {
    let oneMonth = new Date(time).getFullYear() * 12 + (new Date(time).getMonth() + 1)
    let twoMonth = new Date(twoTime).getFullYear() * 12 + (new Date(twoTime).getMonth() + 1)
    // 计算出与最小日期的日时分相同的最大日期的值
    // 如2016-8-17和2019-6-30,计算出来的maxMinTong为2019-6-17
    const { minTime, maxTime, maxMinTong } = getMaxMinDate(time, twoTime, 'day')
    // 计算最小日期到maxMinTong(如2019-6-17)的月份,这样刚好对应月日
    let chaMonth = Math.abs(oneMonth - twoMonth)
    // 如果计算出来的maxMinTong(如2019-6-17)大于最大日期,则表示最后一月没有满,需要减一
    if (new Date(maxMinTong).getTime() > new Date(maxTime).getTime()) {
      chaMonth--
    }
    if (value) {
      return chaMonth - value
    } else {
      return chaMonth
    }
  }
  • 相差天数

getDay()是用于计算相差天数

1.time:比较的第一时间

2.twoTime:比较的第二个时间

3.value:需要减去的天数份(基本上是减去相差足年月换算的天数)

  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)
    }
  }
  • 相差小时

getHour()是用于计算相差小时

1.time:比较的第一时间

2.twoTime:比较的第二个时间

3.value:需要减去的小时(基本上是减去相差足天的小时)

  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)
    }
  }
  • 相差分钟

相差分钟()是用于计算相差分钟

1.time:比较的第一时间

2.twoTime:比较的第二个时间

3.value:需要减去的分钟数(基本上是减去足小时的分钟数)

 
  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)
    }
  }
 
  • 相差秒

getSecond()是用于计算相差秒

1.time:比较的第一时间

2.twoTime:比较的第二个时间

3.value:需要减去的秒数(基本上是减去足分钟的秒数)

  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)
    }
  }
  • 计算相差年月日时分秒(重头戏)

getDffeYMDHMS()是用于计算相差年月日时分秒

1.time:比较的第一时间

2.twoTime:比较的第二个时间

3.详解:

  • a) 首先通过getYear()计算出相差年份
  • b) 通过getMonth()计算出相差月份,但是这个计算出的月份将上面的相差年也是转成成月份数的,因此需要:getMonth(time, twoTime, dffeYear * 12),传入需要减去的年*12
  • c) 通过getDay()计算出相差天数,同上,我们需要减去一些天数(相差年,相差月计算转换成的天数),因此需要:getDay(time, twoTime, dffeDay1),dffeDay1的具体详解可以看详解
  • d) 通过dffeHour()计算出相差的小时,同上需要减去一些时间,因此需要:getHour(time, twoTime, getDay(time, twoTime) * 24)
  • f) 通过getMinute()计算出相差的分钟,同上需要减去一些时间,因此需要:getMinute(time, twoTime, (getDay(time, twoTime) * 24 * 60 + dffeHour * 60))
  • g) 通过getSecond()计算出相差的秒,同上需要减去一些时间,因此需要:getSecond(time, twoTime, (getDay(time, twoTime) * 24 * 60 * 60 + dffeHour * 60 * 60 + dffeMinute * 60))
 // 相差年月日时分秒
  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 }
  }

第三使用方法

将对应的方法放到dayCha()里面

最后使用:

dayCha('2016-8-17','2019-6-15') 
// 2年9月29日  0:0:0
// {
//   chaDay:1032,
//   chaHour:24768,
//   chaMinute: 1486080,
//   chaMonth: 33,
//   chaSecond: 89164800,
//   chaYear: 2,
//   chaDffeYMDHMS:{
//     dffeDay: 29,
//     dffeHour: 0,
//     dffeMinute: 0,
//     dffeMonth: 9,
//     dffeSecond: 0,
//     dffeYear: 2
//   }
// }