-1

From Authentication API along with token i am getting its expiry date time which is in UTC

I am using npm jwt-decode package to extract the info

private setToken(value: string) {
    this._token = value;
    var decoded = jwt_decode(value);
    this._expiry = new Date(decoded['expiry']);
}

To check if token expired i compare it with current UTC dat time

isTokenExpired() {
    var currentUTC = new Date().toUTCString() as any;
    var flag = this._expiry < (currentUTC as Date);
    if (flag)
        return true;
    else
        return false;
}

but new Date().toUTCString() gives strig instaed of date which always retuen false when compare with date object

I tried to convert it to date but after conversion it show date as per browser local time zone instead of actual utc date time

    console.log('current UTC : '+ currentUTC);
    console.log('expiry UTC : '+ this._expiry);
    console.log('utc string to UTC Date : ' + this.createDateUTC(currentUTC));
    console.log(new Date(Date.parse(currentUTC as string)));
    console.log(this._expiry < (currentUTC as Date));

createDateUTC(dateUTC) {
    return new Date(dateUTC + "Z");
}

output is as follow

current UTC : Fri, 02 Sep 2022 02:32:21 GMT
expiry UTC : Fri Sep 02 2022 03:32:08 GMT+0530 (India Standard Time)
utc string to UTC Date : Invalid Date
Fri Sep 02 2022 08:02:21 GMT+0530 (India Standard Time)

Current UTC is correct

expiry UTC is correct , Current UTC + 1 hours as expiry time returned from api

third one is converted to date which is invalid date , Ref : stack overflow Post by Killer

fourth one , tried to parse but it after parsing its local time. UTC time lost.

Any idea how to convert UTC string to date object without loosing its actual value, or any other approach to get current UTC date time

Update 1 :

After refresh

this._expiry = new Date(decoded['expiry'])

inside setTokn() method also lost UTC date time and convert it as per local time

Update 2:

Tried code by "HsuTingHuan" but got same result. Issue persist enter image description here

Saurabh
  • 1,505
  • 6
  • 20
  • 37
  • I found that => if you are just need to compare the date, the date object works fine. Seems the browser will convert to same time zone and compare it. – HsuTingHuan Sep 04 '22 at 10:06

1 Answers1

0

I had met similar case before. Hope my solution can help you

I was used the javascript Date object's toISOString() function or JSON.stringnify()(Because the JSON object will always convert to ISO 8601 format)

And compare all of the datetime object by ISO 8601 format

example works on EDGE:

var currentUTC = new Date().toISOString();

console.log(currentUTC)

function createDateUTC(dateUTC) {
    return new Date(dateUTC);
}

var currentUTCDateobj = createDateUTC(currentUTC)

console.log(currentUTCDateobj)

var expiry = new Date("2022-09-02 00:00:00")

console.log(expiry)

console.log(expiry < currentUTCDateobj);
HsuTingHuan
  • 615
  • 1
  • 7
  • 23
  • Your console.log(currentUTCDateobj) also print current local time, instead of UTC, you can see in Update 2. So issue remain same – Saurabh Sep 02 '22 at 07:04
  • lol. I found that I am using `EDGE`. And the `Chrome` still have the same issue – HsuTingHuan Sep 02 '22 at 07:56