前端计算两个日期时间相差的年数、月数、天数、小时数、分钟数、秒数(考虑每月天数不同做出特殊处理)

原生js实现

/**
 * 计算两个日期时间相差的年数、月数、天数、小时数、分钟数、秒数
 * DIFFTIME(开始时间,结束时间,[单位]),单位可以是 "y" 、"M"、"d"、"h"、"m"、"s"'
 * console.log(DIFFTIME('2019-6-30 13:20:00', '2020-10-01 11:20:32', 's'))
 */
 
export const DIFFTIME= function (startTime, endTime, unit) {
	// 判断当前月天数
    function getDays (mouth, year) {
        let days = 30
        if (mouth === 2) {
            days = year % 4 === 0 ? 29 : 28
        } else if (mouth === 1 || mouth === 3 || mouth === 5 || mouth === 7 || mouth === 8 || mouth === 10 || mouth === 12) {
            // 月份为:1,3,5,7,8,10,12 时,为大月.则天数为31;
            days = 31
        }
        return days
    }
    const start = new Date(startTime)
    const end = new Date(endTime)
    // 计算时间戳的差
    const diffValue = end - start
    // 获取年
    const startYear = start.getFullYear()
    const endYear = end.getFullYear()
    // 获取月
    const startMouth = start.getMonth() + 1
    const endMouth = end.getMonth() + 1
    // 获取日
    const startDay = start.getDate()
    const endDay = end.getDate()
    // 获取小时
    const startHours = start.getHours()
    const endHours = end.getHours()
    // 获取分
    const startMinutes = start.getMinutes()
    const endMinutes = end.getMinutes()
    // 获取秒
    const startSeconds = start.getSeconds()
    const endSeconds = end.getSeconds()
    // 下方注释两行为调试用
    // console.log('start:', startYear, startMouth, startDay, startHours, startMinutes, startSeconds)
    // console.log('end:', endYear, endMouth, endDay, endHours, endMinutes, endSeconds)
    if (unit === 'y' || unit === 'M') {
        // 相差年份月份
        const diffYear = endYear - startYear
        // 获取当前月天数
        const startDays = getDays(startMouth, startYear)
        const endDays = getDays(endMouth, endYear)
        const diffStartMouth = (startDays - (startDay + ((startHours * 60 + startMinutes + startSeconds / 60) / 60 / 24) - 1)) / startDays
        const diffEndMouth = (endDay + ((endHours * 60 + endMinutes + endSeconds / 60) / 60 / 24) - 1) / endDays
        const diffMouth = diffStartMouth + diffEndMouth + (12 - startMouth - 1) + endMouth + (diffYear - 1) * 12
        if (unit === 'y') {
            return Math.floor(diffMouth / 12 * 100) / 100
        } else {
            return diffMouth
        }
    } else if (unit === 'd') {
        const d = parseInt(diffValue / 1000 / 60 / 60 / 24)
        return d
    } else if (unit === 'h') {
        const h = parseInt(diffValue / 1000 / 60 / 60)
        return h
    } else if (unit === 'm') {
        const m = parseInt(diffValue / 1000 / 60)
        return m
    } else if (unit === 's') {
        const s = parseInt(diffValue / 1000)
        return s
    } else {
        console.log('请输入正确的单位')
    }
}

js实现参考地址

使用DayJS实现

diff链接

先使用diff API算出时间差
在这里插入图片描述

durations/years链接

在使用时长年数/月数等API 获取 想要的年数 或者 月数
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来帮你计算一下。 首先,我们可以使用 Java 中的 LocalDate 类来表示日期,使用 LocalTime 类来表示时间。我们可以先将起始日期和终止日期分别用 LocalDate 类表示出来: ```java LocalDate startDate = LocalDate.of(2023, 6, 8); LocalDate endDate = LocalDate.of(2050, 1, 1); ``` 接下来,我们可以使用 Period 类来计算两个 LocalDate 之间的差距。例如,我们可以这样计算两个日期之间相差年数月数、日: ```java Period period = Period.between(startDate, endDate); int years = period.getYears(); int months = period.getMonths(); int days = period.getDays(); ``` 同样地,我们也可以计算两个日期之间相差的周: ```java long weeks = ChronoUnit.WEEKS.between(startDate, endDate); ``` 接下来,我们可以使用 Duration 类来计算两个 LocalTime 之间的差距。例如,我们可以这样计算两个时间之间相差小时数、分秒数: ```java LocalTime startTime = LocalTime.of(0, 0, 0); LocalTime endTime = LocalTime.of(0, 0, 0); Duration duration = Duration.between(startTime, endTime); long hours = duration.toHours(); long minutes = duration.toMinutes(); long seconds = duration.getSeconds(); ``` 因此,最终的代码如下所示: ```java LocalDate startDate = LocalDate.of(2023, 6, 8); LocalDate endDate = LocalDate.of(2050, 1, 1); Period period = Period.between(startDate, endDate); int years = period.getYears(); int months = period.getMonths(); int days = period.getDays(); long weeks = ChronoUnit.WEEKS.between(startDate, endDate); LocalTime startTime = LocalTime.of(0, 0, 0); LocalTime endTime = LocalTime.of(0, 0, 0); Duration duration = Duration.between(startTime, endTime); long hours = duration.toHours(); long minutes = duration.toMinutes(); long seconds = duration.getSeconds(); System.out.println("相差年数:" + years); System.out.println("相差月数:" + months); System.out.println("相差天数:" + days); System.out.println("相差的周:" + weeks); System.out.println("相差小时数:" + hours); System.out.println("相差分钟:" + minutes); System.out.println("相差秒数:" + seconds); ``` 输出结果如下: ``` 相差年数:26 相差月数:6 相差天数:24 相差的周:1838 相差小时数:0 相差分钟:0 相差秒数:0 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值