JS 计算时间差值(天,时,分,秒)

JS 代码

// 传递过来的都是时间戳
function getResidueDate(start_date, end_date){
    let residue = end_date - start_date;
    let days = Math.floor(residue / 1000 / 60 / 60 / 24);
    let hours = Math.floor((residue / 1000 / 60 / 60) % 24);
    let minutes = Math.floor((residue / 1000 / 60) % 60);
    let seconds = Math.floor((residue / 1000) % 60);

	// 不需要补零的话,这部分就可以不用了
    days = days < 10 ? "0" + days : days;
    hours = hours < 10 ? "0" + hours : hours;
    minutes = minutes < 10 ? "0" + minutes : minutes;
    seconds = seconds < 10 ? "0" + seconds : seconds;
    
    return `剩余:${days}${hours}小时${minutes}分钟${seconds}`
}

VUE

<template>
    <div>
        剩余时间:
        <p><span>天:</span>{{ days }}</p>
        <p><span>时:</span>{{ hours }}</p>
        <p><span>分:</span>{{ minutes }}</p>
        <p><span>秒:</span>{{ seconds }}</p>
    </div>
</template>

<script>
export default {
    name: "Test",
    data: function () {
        return {
            start_date: "2021-09-02 15:45:30", // 开始时间
            end_date: new Date(), // 结束时间(时间戳)
            days: 0,
            hours: 0,
            minutes: 0,
            seconds: 0,
        };
    },
    computed: {
        residue: function () {
            return this.end_date - new Date(this.start_date).getTime();
        },
    },
    created() {
        this.getResidueDate();
    },
    methods: {
        getResidueDate() {
            this.days = this.addZero(
                Math.floor(this.residue / 1000 / 60 / 60 / 24)
            ); //天
            this.hours = this.addZero(
                Math.floor((this.residue / 1000 / 60 / 60) % 24)
            ); //时
            this.minutes = this.addZero(
                Math.floor((this.residue / 1000 / 60) % 60)
            ); //分
            this.seconds = this.addZero(Math.floor((this.residue / 1000) % 60)); //秒
        },
        addZero(d) {
            return parseInt(d) < 10 ? "0" + d : d;
        },
    },
};
</script>
### 如何使用JavaScript计算两个日期之间的时间差 在JavaScript中,可以通过操作`Date`对象来计算两个日期之间的时间差。以下是具体实现方式: #### 初始化日期对象 通过创建`Date`对象表示具体的日期和时间。如果需要比较特定的日期,则可以直接传入日期字符串或参数列表。 ```javascript const date1 = new Date('2023-10-01T00:00:00'); // 起始日期 const date2 = new Date('2023-10-10T00:00:00'); // 结束日期 ``` #### 计算时间戳差异 利用`Date`对象的毫时间戳特性,直接相减得到两者的毫数差异[^3]。 ```javascript const timeDifferenceInMilliseconds = Math.abs(date2 - date1); ``` #### 将毫转换为其他单位 为了更直观地表达时间差,通常将其转化为、小钟或的形式。以下是一个通用函数用于完成这一目标: ```javascript function calculateTimeDifference(startDate, endDate) { const differenceInMilliseconds = Math.abs(endDate - startDate); const seconds = Math.floor(differenceInMilliseconds / 1000); // const minutes = Math.floor(seconds / 60); // 钟 const hours = Math.floor(minutes / 60); // 小 const days = Math.floor(hours / 24); // return { days, hours, minutes, seconds }; } // 使用示例 const result = calculateTimeDifference(date1, date2); console.log(`相差 ${result.days} , ${result.hours % 24} 小, ${result.minutes % 60} 钟`); ``` 上述代码实现了从毫到更高层次时间单位的转化,并返回一个包含各时间维度的对象[^4]。 #### 特殊情况处理 当涉及跨月份或年的复杂场景,需额外注意边界条件。例如,在某些情况下可能仅关注月份差而非精确数。此可采用如下逻辑: ```javascript function monthDifference(startMonthYear, endMonthYear) { let months; months = (endMonthYear.getFullYear() - startMonthYear.getFullYear()) * 12; months -= startMonthYear.getMonth(); months += endMonthYear.getMonth(); return months <= 0 ? 0 : months; } ``` 此方法适用于统计项目周期或其他基于月度的需求[^5]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值