【发布时间】:2021-10-12 19:16:22
【问题描述】:
在本地,一切都是精确的。一旦部署到 Heroku,时间差大约 6 小时。我不希望转换 Heroku 时区(希望它保持 UTC)。我已经尝试了从 getTimezoneOffset() 转换到不同日期格式的所有方法,但我仍然得到相同的结果。我怎样才能让这两个日期时间相互匹配,而不是在部署时被小时数抵消?当格式完全相同时,为什么它们会有所不同?
// Used to calculate current date time
const currentDate = new Date();
// ^ Production - (2021-10-12T19:12:41.081Z)
const time = `${currentDate.getHours()}:${currentDate.getMinutes()}`;
const fullDate = `${currentDate.getMonth()}/${currentDate.getDate()}/${currentDate.getFullYear()}`;
const currentDateFormatted = new Date(`${fullDate} ${time}`);
// ^ Production - (2021-10-12T19:12:00.000Z)
const currentParsedDateToUTC = Date.parse(currentDateFormatted.toUTCString());
// Used to calculate an event date time
const eventDate = new Date(`${event.date} ${event.endTime}`); // same exact format as above
// ^ Production - (2021-10-12T13:12:00.000Z)
const eventParsedDateToUTC = Date.parse(eventDate.toUTCString());
const isExpired = (currentParsedDateToUTC > eventParsedDateToUTC); // works locally, but not in production
在此示例中,事件日期和开始时间与当前日期时间相同。我怎样才能防止它们大不相同?
【问题讨论】:
-
这段代码是否在客户端运行(在浏览器中)?还是服务器端(在node.js进程中)?
-
所有运行的服务器端,Node.js @DeclanMcKelvey-Hembree
-
event.date和event.endTime的来源是什么?事件对象来自哪里?
-
它直接来自数据库。 event.date = 10/12/2021 和 event.endTime = 13:12。这就像它将当前的新日期对象转换为 UTC,而不是使用事件创建的日期对象。 @DeclanMcKelvey-Hembree
-
我已经更新了我的帖子,以展示最初的新日期在生产中的样子。会不会和结束时区有关? (081Z) @DeclanMcKelvey-Hembree
标签: javascript date heroku time