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

Viewed 114486

How to set this cookie to expire in one hour from the current time:

document.cookie = 'username=' + value; + 'expires=' + WHAT GOES HERE?; + 'path = /';
3 Answers

Instead of using expires, you can use max-age. Max-Age takes presence over expires and accepts an expiration duration in seconds.

For your example, considering an hour is 3600 seconds, the code would be:

document.cookie = 'username=' + value + '; max-age=3600; path=/';
Related