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

3
  • 2
    What does this._accessToken[".expires"] contain? Commented Oct 16, 2013 at 15:45
  • 3
    @ComFreek "Wed, 16 Oct 2013 15:42:53 GMT", as the first console.log() call says. Commented Oct 16, 2013 at 15:47
  • The better question would be "When did you execute this?" :-) Btw, the log looks like the token did contain a Date object, not a string. Commented Oct 16, 2013 at 16:52

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.