在JS中将刻度转换为日期

Vin*_*nce 3 javascript jwt typescript

我正在尝试检查JWT令牌的到期日期,而我尝试执行的所有操作均未获得正确的日期。

"exp": 1522210228 => real answer => Wednesday, March 28, 2018 12:10:28 AM
Run Code Online (Sandbox Code Playgroud)

我尝试过那些libs,但没有使它们起作用...

  1. https://github.com/auth0/angular2-jwt/blob/master/src/jwthelper.service.ts
  2. https://github.com/auth0/jwt-decode

1个

const helper = new JwtHelperService();
const decodedToken = helper.decodeToken(this.authentificationInfos.token);
const expirationDate = helper.getTokenExpirationDate(this.authentificationInfos.token);

console.log(expirationDate); => null?
Run Code Online (Sandbox Code Playgroud)

2

import * as decode from 'jwt-decode';

const token = decode<{ data: { exp: number, iat: number, iss: string, nbf: number, username: string } }>(this.authentificationInfos.token);
const date = new Date(token.data.exp);
console.log(date); => Sun Jan 18 1970 09:50:10 GMT-0500 (Eastern Standard Time)

const d = new Date(0);
d.setUTCMilliseconds(token.data.exp);
console.log(d); => Sun Jan 18 1970 09:50:10 GMT-0500 (Eastern Standard Time)
Run Code Online (Sandbox Code Playgroud)

这是完整的令牌:

eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJkYXRhIjp7InVzZXJuYW1lIjoiYmlsb2RlYXV2aW5jZW50QG91dGxvb2suY29tIiwiaWF0IjoxNTIyMjA2NjI4LCJpc3MiOiJtbnAuY29tIiwibmJmIjoxNTIyMjA2NjI4LCJleHAiOjE1MjIyMTAyMjh9fQ.1WRlQatauXw2HEWj9B9VL6fIVR-4nAoKuWvkS4_m86k
Run Code Online (Sandbox Code Playgroud)

https://jwt.io/正在解码令牌,并且显示的exp是正确的。

如何从token.exp获取真实日期?

acd*_*ior 6

由于它是 epoch 的秒值,您要做的就是乘以exp1000。

请参阅下面的演示。

const jwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IllvIiwiZXhwIjoxNTE2MjM5MDIyfQ.9xA3RWBjEdQmUFlf5E7fLrR8Xi36ogcjGrOdkL6DA3Y";

const decodedJwt = jwt_decode(jwt);

console.log( new Date(decodedJwt.exp * 1000) );

// output: "2018-01-18T01:30:22.000Z"
Run Code Online (Sandbox Code Playgroud)
<script src="https://unpkg.com/jwt-decode@3.1.2/build/jwt-decode.js"></script>
Run Code Online (Sandbox Code Playgroud)


jps*_*jps 5

JWT中的时间戳是UNIX时间戳,从1970年1月1日00:00 UTC开始计数:https : //tools.ietf.org/html/rfc7519#section-4.1.4解释了数字日期用于exp声明(以及对于nbf(不早于)和iat(在发出)索赔)

https://tools.ietf.org/html/rfc7519#section-2定义数字日期:

一个JSON数值,表示从1970-01-01T00:00:00Z UTC到指定的UTC日期/时间为止的秒数,而忽略了leap秒。

var jwtDecode = require('jwt-decode');
var jwt = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJkYXRhIjp7InVzZXJuYW1lIjoiYmlsb2RlYXV2aW5jZW50QG91dGxvb2suY29tIiwiaWF0IjoxNTIyMjA2NjI4LCJpc3MiOiJtbnAuY29tIiwibmJmIjoxNTIyMjA2NjI4LCJleHAiOjE1MjIyMTAyMjh9fQ.1WRlQatauXw2HEWj9B9VL6fIVR-4nAoKuWvkS4_m86k";

const token = jwtDecode(jwt);
const d = new Date(0);
d.setUTCSeconds(token.data.exp);
console.log(d);
Run Code Online (Sandbox Code Playgroud)

输出:

2018-03-28T04:10:28.000Z

使用d.getHours()d.getMinutes()等等,让您的本地时间。

  • 正在使用“new Date(exp)”并获取 1970 年的日期,但使用所描述的“setUTCSeconds”效果很好。 (2认同)

小智 5

如果您在 1970 年得到答案,您可能不会将解码后的 exp 值乘以所需的 1000。创建新的 Date() 对象时,您需要将以毫秒为单位的到期日期传递给它。

const token = jwtDecode(jwt);
const d = new Date(token.exp * 1000);
Run Code Online (Sandbox Code Playgroud)