[英]Moment time is returning UTC time instead of local time
目前,我正在开发一个项目,以在用户单击应用程序中的下载报告按钮时创建 excel 报告。 除了正在下载的文件名外,一切正常。
问题是当我们从美国检查时(CST Time with Daylight Savings),假设如果用户在 11 月 26 日晚上 9:00 下载报告,则文件名为 2020 年 11 月 27 日。我们在 node.js 应用程序中使用时刻得到时间。
const date = moment().format('LL');
有人可以知道我在哪里失踪。 如果我是正确的,那 moment() function 将返回本地时间而不是 UTC 时间。 但似乎得到的是UTC时间而不是当地时间。
我们是否需要使用 moment().local() 来代替?
任何帮助将非常感激。
提前致谢。
我想您应该在 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')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.