Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transform Tick to Date in JS

I'm trying to check the expiration date of a JWT token and everything I tried is not getting me the right date.

"exp": 1522210228 => real answer => Wednesday, March 28, 2018 12:10:28 AM

I've tried thoses libs and I didn't get those to work...

  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?

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)

Here is the complete token:

eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJkYXRhIjp7InVzZXJuYW1lIjoiYmlsb2RlYXV2aW5jZW50QG91dGxvb2suY29tIiwiaWF0IjoxNTIyMjA2NjI4LCJpc3MiOiJtbnAuY29tIiwibmJmIjoxNTIyMjA2NjI4LCJleHAiOjE1MjIyMTAyMjh9fQ.1WRlQatauXw2HEWj9B9VL6fIVR-4nAoKuWvkS4_m86k

https://jwt.io/ is decoding the token and the exp displayed is correct.

How can I get the real date from token.exp?

like image 345
Vince Avatar asked Apr 03 '18 01:04

Vince


People also ask

How do you get a tick out of a date?

To convert a date object to ticks:Use the getTime() method on the date to get the number of milliseconds since the Unix Epoch. Multiply the timestamp by the number of ticks in a millisecond (10,000).

What is date () JavaScript?

A JavaScript date is fundamentally specified as the number of milliseconds that have elapsed since the ECMAScript epoch, which is defined as January 1, 1970, UTC (equivalent to the UNIX epoch).

How long is a tick in JavaScript?

A single tick represents one hundred nanoseconds or one ten-millionth of a second.

Is there a date type in JavaScript?

JavaScript does not have a date data type. However, you can use the Date object and its methods to work with dates and times in your applications. The Date object has a large number of methods for setting, getting, and manipulating dates. It does not have any properties.


3 Answers

The timestamps in JWT are UNIX timestamps counting from 01.01.1970 00:00 UTC: https://www.rfc-editor.org/rfc/rfc7519#section-4.1.4 explains that a numeric date is used for the exp claim (and also for the nbf (not before) and iat (issued at) claims)

https://www.rfc-editor.org/rfc/rfc7519#section-2 defines the numeric date:

A JSON numeric value representing the number of seconds from 1970-01-01T00:00:00Z UTC until the specified UTC date/time, ignoring leap seconds.

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);

output:

2018-03-28T04:10:28.000Z

Use d.getHours(), d.getMinutes() etc. to get your local time.

like image 126
jps Avatar answered Oct 21 '22 17:10

jps


Since it is a value of seconds from epoch, all you have to do is multiply exp by 1000.

See demo below.

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

const decodedJwt = jwt_decode(jwt);

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

// output: "2018-01-18T01:30:22.000Z"
<script src="https://unpkg.com/[email protected]/build/jwt-decode.js"></script>
like image 43
acdcjunior Avatar answered Oct 21 '22 17:10

acdcjunior


import jwt_decode from 'jwt-decode';

const token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IllvIiwiZXhwIjoxNTE2MjM5MDIyfQ.9xA3RWBjEdQmUFlf5E7fLrR8Xi36ogcjGrOdkL6DA3Y"
let _expData = jwt_decode(token || '{}')
let _exp = token.exp * 1000
let _date = new Date(_exp)
console.log(_date)
like image 22
OnceACurious Avatar answered Oct 21 '22 15:10

OnceACurious