获取两个Date对象之间的持续时间(以小时和分钟为单位)-JavaScript

时间:2018-10-18 06:23:48

标签: javascript datetime timezone utc

尽管如此,我在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);

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

1 个答案:

答案 0 :(得分:2)

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