首页 > 解决方案 > 获取两个 Date 对象之间的持续时间(以小时和分钟为单位) - JavaScript

问题描述

虽然,我在这里看到了多个类似的问题,但没有一个可以帮助我弄清楚我的计算出了什么问题。我知道我可以使用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);

我期望控制台日志中有正值,但我得到了负值。我错过了什么吗?

标签: javascriptdatetimetimezoneutc

解决方案


问题源于 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]));

完整片段:

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);


推荐阅读