Ionic 4 returns different time value for Android & iOS

Viewed 323

Here's my code:

var get_time = new Date(my_time.replace(' ','T')).getTime();

The values are different for Android & iOS.

I think there is some interference of timezones

2 Answers

I am not sure if it is the best way, You can convert the date to utc and set the time by adding the timezone offset

Try the following

get_time= new Date(new Date(get_time.replace(' ','T')+'Z').getTime()).setMinutes(new Date(new Date(get_time.replace(' ','T')+'Z').getTime()).getMinutes() + new Date(new Date(get_time.replace(' ','T')+'Z').getTime()).getTimezoneOffset());

It will provide the correct time according to your local time.

If you just put a 'T' instead of the space in the datetime string then it interprets correctly on iOS.

let iOSString = parsed_token.expires_at.replace(' ', 'T');

if(new Date(`${iOSString}`).getTime()/1000 > new Date().getTime()/1000) {
    // Handle the redirection for an invalid token.
}
Related