【问题标题】:Get duration (in Hours and minutes) between two Date objects - JavaScript获取两个 Date 对象之间的持续时间(以小时和分钟为单位) - JavaScript
【发布时间】:2019-03-22 21:55:38
【问题描述】:

虽然,我在 SO 上看到了多个类似的问题,但没有一个可以帮助我弄清楚我的计算出了什么问题。我知道我可以使用诸如 Moment.JS 之类的库来简化我的解决方案,但我只想要原生 JavaScript 解决方案。

我正在尝试计算两个 Date 对象之间的持续时间(以小时和分钟为单位),但我得到了负数(不正确)的持续时间。

function padNumber(number, width = 2, padWith = '0') {
    const strNum = number.toString();
    return strNum.length >= width ? strNum : new Array(width - strNum.length + 1).join(padWith) + strNum;
}

// Get UTC date time from PHP date (Y-m-d) and time (H:i:s) strings
function getUTCDateTime(date, time, timezoneOffset = -480) {
    const dateParts = date.split('-').map((el) => Number(el)); // Y-m-d
    const timeParts = time.split(':').map((el) => Number(el)); // H:i:s
    const dateTimeUTC = new Date(Date.UTC(dateParts[0], dateParts[1], dateParts[2], timeParts[0], timeParts[1], timeParts[2]));
    // Set back Singapore specific time (GMT+8:00)
    dateTimeUTC.setUTCHours(dateTimeUTC.getUTCHours() + timezoneOffset / 60);
    return dateTimeUTC;
}

function getDuration(timeStart, timeEnd = new Date()) {
    const msDiff = timeEnd.getTime() - timeStart.getTime();
    const minDiff = msDiff / 60000;
    const hourDiff = Math.floor(msDiff / 3600000);
    return {
        hours: this.padNumber(hourDiff, 2),
        minutes: this.padNumber(Math.floor(minDiff - 60 * hourDiff), 2)
    };
}

// Got from server (in Singapore timezone)
const serverDate = '2018-10-18';
const serverTime = '00:22:51';

// Convert server date and time (timezone specific) strings to Date object
const serverUTC = getUTCDateTime(serverDate, serverTime);

// Get duration between server time and now
const duration = getDuration(serverUTC);

// Expected positive value but getting negative as server time is in past
console.log(duration);

我期望控制台日志中的值为正值,但结果为负值。我错过了什么吗?

【问题讨论】:

    标签: javascript datetime timezone utc


    【解决方案1】:

    问题源于 JavaScript 中月份从零开始的事实(即,一月是 0,二月是 1,等等)。 getUTCDateTime() 中的日期构造没有考虑到这一点。

    这一行:

    const dateTimeUTC = new Date(Date.UTC(dateParts[0], dateParts[1], dateParts[2], timeParts[0], timeParts[1], timeParts[2]));
    

    应该是:

    const dateTimeUTC = new Date(Date.UTC(dateParts[0], dateParts[1] - 1, dateParts[2], timeParts[0], timeParts[1], timeParts[2]));
    

    完成sn-p:

    function padNumber(number, width = 2, padWith = '0') {
        const strNum = number.toString();
        return strNum.length >= width ? strNum : new Array(width - strNum.length + 1).join(padWith) + strNum;
    }
    
    // Get UTC date time from PHP date (Y-m-d) and time (H:i:s) strings
    function getUTCDateTime(date, time, timezoneOffset = -480) {
        const dateParts = date.split('-').map((el) => Number(el)); // Y-m-d
        const timeParts = time.split(':').map((el) => Number(el)); // H:i:s
        const dateTimeUTC = new Date(Date.UTC(dateParts[0], dateParts[1] - 1, dateParts[2], timeParts[0], timeParts[1], timeParts[2]));
        // Set back Singapore specific time (GMT+8:00)
        dateTimeUTC.setUTCHours(dateTimeUTC.getUTCHours() + timezoneOffset / 60);
        return dateTimeUTC;
    }
    
    function getDuration(timeStart, timeEnd = new Date()) {
        const msDiff = timeEnd.getTime() - timeStart.getTime();
        const minDiff = msDiff / 60000;
        const hourDiff = Math.floor(msDiff / 3600000);
        return {
            hours: this.padNumber(hourDiff, 2),
            minutes: this.padNumber(Math.floor(minDiff - 60 * hourDiff), 2)
        };
    }
    
    // Got from server (in Singapore timezone)
    const serverDate = '2018-10-18';
    const serverTime = '00:22:51';
    
    // Convert server date and time (timezone specific) strings to Date object
    const serverUTC = getUTCDateTime(serverDate, serverTime);
    
    // Get duration between server time and now
    const duration = getDuration(serverUTC);
    
    // Expected positive value but getting negative as server time is in past
    console.log(duration);

    【讨论】:

    • 哦,我知道这一点:),但只是在计算时忘记了照顾。感谢您指出这一点。
    猜你喜欢
    • 2013-07-06
    • 2011-04-19
    • 2020-01-25
    • 1970-01-01
    • 1970-01-01
    • 2021-01-04
    • 2013-05-14
    • 2017-03-25
    • 2021-05-14
    相关资源
    最近更新 更多