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