2017-08-25 18 views
0

出于某种原因,当我从数据库中检索先前保存的日期,并尝试将其转换为我的本地时间,它不同于当前时间。moment.js - 哪一个会是正确的输出,考虑到我的时区是GMT-3?

这是我的代码:

// got accessToken from database 
console.log(moment(accessToken.expiresAt).toDate()); 
console.log(moment().toDate()); 

输出是:

Fri Aug 25 2017 20:47:51 GMT-0300 (BRT) 
Fri Aug 25 2017 17:47:51 GMT-0300 (BRT) 

考虑到我目前的本地时间为25月/ 8月/ 2017年17点47分51秒,而且accessToken.expiresAt被保存在同一个请求中(同时),哪一个会是正确的输出?

如果有人有,为什么我的存储时间(存储在MySQL时间戳)从当前时间不同的任何想法,请带我到一个方向。

预先感谢您。

+4

什么显示'的console.log(accessToken.expiresAt)'? – alexmac

+2

我的猜测是你的输入是基于UTC,但不确定是这样。没有看到你确切的输入('accessToken.expiresAt'),这是不可能的。 –

回答

1

Moment.js人大概都会认为你保存的访问令牌的时间是UTC - 试试这个:

console.log(moment(accessToken.expiresAt).utc().toDate()); 

您可以通过在一个日期转换器,输入您的时间戳检查您的本地时区与UTC之间的差别作为http://www.convertunixdate.com - 让你的时间戳,执行此:

moment(accessToken.expiresAt).unix() 
相关问题