JavaScript中处理时间差

ES6版本

const countdown = (endTime, includeSeconds = true) => {
    // 获取当前时间
    const now = new Date();
    // 将传入的结束时间字符串转换为日期对象
    const endDateTime = new Date(endTime);
 
    // 检查传入的时间字符串是否只包含日期(不包含时分秒)
    if (endTime.trim().split(' ').length === 1) {
        // 如果只有日期,则将时间设置为该日期的午夜(00:00:00)
        endDateTime.setHours(0, 0, 0, 0);
    }
 
    // 计算当前时间与结束时间的时间差(单位:毫秒)
    const timeDifference = endDateTime - now;
 
    // 计算剩余的天数、小时数、分钟数和秒数
    const days = Math.floor(timeDifference / (24 * 3600 * 1000));
    const hours = Math.floor((timeDifference % (24 * 3600 * 1000)) / (3600 * 1000));
    const minutes = Math.floor((timeDifference % (3600 * 1000)) / (60 * 1000));
 
    // 构建倒计时对象
    const countdownObject = {
        days,
        hours,
        minutes,
    };
    
    // 根据includeSeconds参数决定是否包含秒数
    if (includeSeconds) {
        countdownObject.seconds = Math.floor((timeDifference % (60 * 1000)) / 1000);
    }
    
    return countdownObject;
}
 
console.log(countdown('2078-01-28')); // {days: 19775, hours: 8, minutes: 41, seconds: 9}
console.log(countdown('2078-01-28 12:30:00')); // {days: 19775, hours: 21, minutes: 11, seconds: 9}
console.log(countdown('2078-01-28', false)); // {days: 19775, hours: 8, minutes: 41}

ES5版本 

function countdown(endTime, includeSeconds) {
    // 设置默认值,如果includeSeconds未定义,则默认为true
    includeSeconds = typeof includeSeconds !== 'undefined' ? includeSeconds : true;

    // 获取当前时间
    var now = new Date();
    // 将传入的结束时间字符串转换为日期对象
    var endDateTime = new Date(endTime);

    // 检查传入的时间字符串是否只包含日期(不包含时分秒)
    if (endTime.trim().split(' ').length === 1) {
        // 如果只有日期,则将时间设置为该日期的午夜(00:00:00)
        endDateTime.setHours(0, 0, 0, 0);
    }

    // 计算当前时间与结束时间的时间差(单位:毫秒)
    var timeDifference = endDateTime - now;

    // 计算剩余的天数、小时数、分钟数和秒数
    var days = Math.floor(timeDifference / (24 * 3600 * 1000));
    var hours = Math.floor((timeDifference % (24 * 3600 * 1000)) / (3600 * 1000));
    var minutes = Math.floor((timeDifference % (3600 * 1000)) / (60 * 1000));

    // 构建倒计时对象
    let countdownObject = {
        days: days,
        hours: hours,
        minutes: minutes,
    };
    
    // 根据includeSeconds参数决定是否包含秒数
    if (includeSeconds) {
        countdownObject.seconds = Math.floor((timeDifference % (60 * 1000)) / 1000);
    }
    
    return countdownObject;
}

console.log(countdown('2078-01-28')); // {days: 19775, hours: 8, minutes: 44, seconds: 47}
console.log(countdown('2078-01-28 12:30:00')); // {days: 19775, hours: 21, minutes: 14, seconds: 47}
console.log(countdown('2078-01-28', false)); // {days: 19775, hours: 8, minutes: 44}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦境游子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值