javascript - 获取两个 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);
我期望控制台日志中有正值,但我得到了负值。我错过了什么吗?
解决方案
问题源于 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);
推荐阅读
- neo4j - 由于“名称”的属性值为空,无法合并以下节点:
- google-app-engine - 从谷歌工件注册表在应用引擎中安装私有包
- bash - 在 vi 或 vim 等虚拟 tty 中打开脚本
- javascript - JSP中java变量内的Javascript变量
- jenkins - 如何在Jenkins管道中通过迭代将shell输出重定向到变量
- python - 子进程不将输出存储在变量中
- javascript - 将项目数组展平到另一个数组的中间
- error-handling - 终端错误:没有这样的文件或目录:/dev/fd/12export - 线程“主”在“打印到标准输出失败:管道损坏(操作系统错误 32)时惊慌失措
- pandas - 熊猫:根据日期列创建一个时期
- c# - 在 Xamarin 跨平台应用程序中使用本机页面