2

I have to do the logic for when to renew a refresh token and somehow I do not get the correct values.

 console.log(this._accessToken[".expires"]);
 console.log(Date.parse(this._accessToken[".expires"]) > new Date().getTime());

results in

Wed, 16 Oct 2013 15:42:53 GMT 
true 

It should return false.

How to i make the new Date() be in the same timezone as the string. (I assume the string has all the information it need to tell if the current time is before or after it).

Poul K. Sørensen
  • 16,950
  • 21
  • 126
  • 283

2 Answers2

3

You shouldn't be worrying about timezones if you're comparing Unix timestamps (see this answer as to why).

You're doing the comparison correctly -- you're getting true instead of false because that's correct:

> new Date()
Wed Oct 16 2013 11:51:29 GMT-0400 (EDT)
> Date.parse("Wed, 16 Oct 2013") > new Date().getTime()
false
> Date.parse("Wed, 16 Oct 2013 00:00:00 GMT") > new Date().getTime()
false
> Date.parse("Wed, 16 Oct 2013 23:00:00 GMT") > new Date().getTime()
true
Community
  • 1
  • 1
Christian Ternus
  • 8,406
  • 24
  • 39
0

You can simply create a new date with the string your database returns and compare them using the usual operators:

var expirationDate = new Date("Wed, 16 Oct 2013 15:42:53 GMT");
var now = new Date();
alert(now > expirationDate);

JSFiddle

Bucket
  • 7,415
  • 9
  • 35
  • 45