【问题标题】:Moment time is returning UTC time instead of local time时刻时间正在返回 UTC 时间而不是本地时间
【发布时间】:2021-03-09 22:19:43
【问题描述】:

目前,当用户单击从应用程序下载报告按钮时,我正在开发一个创建 excel 报告的项目。除了下载的文件名外,一切正常。

问题是当我们从美国检查时(CST Time with Daylight Savings),假设如果用户在 11 月 26 日晚上 9:00 下载报告,则文件名为 2020 年 11 月 27 日。我们正在使用时刻用于获取时间的 node.js 应用程序。

const date = moment().format('LL');

谁能知道我在哪里失踪。如果我是正确的,那么 moment() 函数将返回本地时间而不是 UTC 时间。但似乎获取的是 UTC 时间而不是本地时间。

我们需要使用 moment().local() 来代替吗?

任何帮助将不胜感激。

提前致谢。

【问题讨论】:

  • 您是否检查了运行后端应用程序的服务器上的时区?
  • @Anatoly 代码在 aws 无服务器中。这是我们-west2
  • moment.locale() 返回什么?
  • 你试过了吗time-zones
  • @Anatoly 重新调整 en.

标签: javascript node.js momentjs moment-timezone


【解决方案1】:

我想你应该在 URL 中传递一个用户时区偏移量:

// -300 - (CST with daylight savings)
https://somesite.com/api/report?offset=-300 

并在后端使用 moment.js 更改偏移量,如下所示:

// old versions of a moment.js
const date = moment().zone(offset).format('LL')
// new versions of a moment.js
const date = moment().utcOffset(offset).format('LL')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-06
    • 1970-01-01
    • 1970-01-01
    • 2015-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多