0

I have a token and I want to save it in cookie for only 1 hour.

Here is my code :

        time= 3600 * 1000;
    let expires = new Date();
    expires.setDate(expires + time);
    console.log(expires);
    document.cookie =`${name}=${value};expires=${expires.toUTCString()};path=/`;

When I run it in chrome I see the following:

Invalid Date

For more info I use the following for implementing my function:

How to set a cookie to expire in 1 hour in Javascript?

Can anyone shed light on this? what am I doing wrong?

Hamed Minaee
  • 2,480
  • 4
  • 35
  • 63

1 Answers1

0

This is happening because of the expires+time part. You can't add a date to a number like that. You could try something like

let expires=new Date(new Date().valueOf()+time);
sbrass
  • 905
  • 7
  • 12