As the access token is valid for 20 mins in SFMC, I want to reuse the access token. Hence I tried to write the below code.

What I am doing currently

  1. I have created a Data Extension called - tokens_DE and store the fields - accesstoken, startDate, endDate, BU
  2. Then in code, before making any API call first I want to check if expireDate > now() —> which means expired

But its not working. The code always gets into else statement —> not expired even though it is expired.

set @now= now(1)
set @nowPlus1Min   = DateAdd(@now, 1, "MI")

<script runat=server>
                 var now = Variable.GetValue("@now");
                 var nowPlus1Min = Variable.GetValue("@nowPlus1Min");
                 var bu =“US”
                 var expireDate =  Platform.Function.Lookup("ent.tokens_DE","EndDate","BU",bu)
                 if(expireDate > now)
                    //invoke the resusable getaccesstoken function..
                    //var jsonresp     = getAccessToken();
                    var accessToken  = jsonresp.access_token;
                    var restURL      = jsonresp.rest_instance_url;
                    //Write("accesstoken :"+accessToken+"");
                    //Write("REST URL :"+restURL+"");
                   var upsert = Platform.Function.UpsertDE("tokens_DE”, ["BU"], ["US"],["startDate","EndDate","accessToken"],[now,nowPlus1Min,accessToken]); 
                    Write("not expired...")
                    var accessToken=Platform.Function.Lookup("tokens_DE","accessToken","BU",bu)

Please can anyone help me figure out a way to fix this? It looks the comparison is the issue. please can anyone tell me how to compare timestamp of 2 values in SSJS?

1 Answer 1


The general comparison logic is correct, I would assume the problem hides in something else.
Suggestions to troubleshoot:

  1. Make sure "tokens_DE" Data Extension has fields "startDate" and "endDate" as Date datatypes
  2. Print out SSJS variables now and expireDate with Write("now: " + now + "<br>" + "expireDate: " + expireDate); to see if you are correctly picking values for further comparison
  3. Use typeOf(); function to make sure both values are dates to make a further valid comparison of both
  4. Not sure whether this is a typo but var bu =“US” is an invalid format and it cannot use curly double-quotes for setting a value, thus set it as var bu = "US"

Tips on top:

  • You do not need to use AMPscript to set a timestamp into a variable and then add one minute, you can do it in JS this way:
var now = Now();
var nowPlus1Min = now.setTime(now + 1000 * 60); // 1k milliseconds = 1 second
  • Lastly, better to use double-quotes in runat="server" part due to the already found issues with that.
  • I do not see how you set up your token expiry date but in fact, it has 18 minutes validity:

Length of time in seconds that the token is valid: approximately 1080 seconds, or 18 minutes. The actual access token lifetime is 20 minutes, but the expires_in setting is 18 minutes because we recommend that you refresh your token two minutes before its lifetime ends.

  • Thank you very much for the clear and detailed answer. Your suggestions for troubleshooting helped and the problem was with "startDate" and "endDate" were not Date datatypes.
    – natlin
    Jun 3 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.