瞬间将JS转换为用户的本地时间

时间:2018-11-14 22:08:47

标签: node.js timezone momentjs

以为我了解如何将存储在数据库中的时间转换为用户本地时间,但我似乎不知道。当我将启动时间保存到我的postresql数据库(在Heroku中)中时,它以2018-11-14 19:45:00的形式提供,我将其保存为TIMESTAMP。在保存时,我应该花点时间吗?

运行Heroku日志我已经注意到服务器时间是2018-11-14T19:45:00.200076+00:00,即UTC吗?

在我的节点应用程序中使用JS时刻,我正在执行以下操作

fixture.kick_off = 2018-11-14 19:45:00.000000
<%=  moment(fixture.kick_off).local().format('HH:mm A') %>

我有一个来自丹麦的用户说这显示为19:45 pm,我希望按时差显示20:45 PM

我在这里误会了什么吗(很有可能)

谢谢

更新

我现在按照马特的答案使用以下内容

<%=  moment.utc(fixture.kick_off).local().format('HH:mm A') %>

根据马特(Matt)关于检查fixof.kick_off的类型的评论,它返回了object,现在我的代码已更新为

<%  var kick_off = fixture.kick_off.toString() %>
<%=  moment.utc(kick_off).local().format('HH:mm') %>

哪个在控制台中返回以下内容

Deprecation warning: value provided is not in a recognized RFC2822 or ISO 
format. moment construction falls back to js Date(), which is not reliable 
across all browsers and versions. Non RFC2822/ISO date formats are discouraged 
and will be removed in an upcoming major release. Please refer to 
http://momentjs.com/guides/#/warnings/js-date/ for more info.
Arguments:
[0] _isAMomentObject: true, _isUTC: true, _useUTC: true, _l: undefined, _i: Thu 
Nov 15 2018 19:00:00 GMT+0000 (Greenwich Mean Time), _f: undefined, _strict: 
undefined, _locale: [object Object]
Error

1 个答案:

答案 0 :(得分:0)

假设基于UTC的时间存储在数据库中,您只是忘了告诉那一刻。使用moment.utc(input)代替moment(input)

moment.utc(fixture.kick_off).local().format('HH:mm A')
相关问题