如何处理 JWT.exp 时间?

Isa*_*aac 5 javascript date jwt momentjs

console.log('DEBUG::+jwtDecode(token).exp', +jwtDecode(token).exp); //1534820211

console.log('DEBUG::try', new Date(+jwtDecode(token).exp).toISOString());
//DEBUG::try 1970-01-18T18:20:20.211Z
Run Code Online (Sandbox Code Playgroud)

我有一个有价值的令牌,1534820211当我尝试使用toISOString()它转换它时,它给了我 year 1970-01-18T18:20:20.211Z

但是当我在jwt.io解码相同的标记,并将鼠标悬停在 上时exp,它显示出2018-08-21....巨大的差异。我也试图通过jwtDecode(token).exp进入moment和使用的格式,还是回到我的日期时间1970xxxx

moment(jwtDecode(token).exp).format();
Run Code Online (Sandbox Code Playgroud)

31p*_*piy 16

您拥有的值是epoch 的秒数

JavaScriptDate构造函数(以及moment构造函数)从 epoch接受以毫秒为单位的值。将数字乘以 1000,您的代码应该可以正常工作:

var exp = 1534820211 * 1000;
console.log(new Date(exp));
Run Code Online (Sandbox Code Playgroud)