Set cookies to expire in hours, minutes, seconds - Javascript

I am using a javascript function, that allows to set after how many days cookies should expire:

var date=new Date();
var expirydays=30;
date.setDate(date.getDate()+expirydays);


Now, I need to set cookies to expire after a specific number of hours, minutes or seconds. Can you please give me advice how to do it ?
posted on 13.06.2019 at 16:38
0pnshow more
onefiveonefive
Hi,

To set a cookie expiring in specific amount of hours, minutes and seconds, you can use the solution below. The cookie will expire in 1 hour, 5 minutes and 30 seconds. Expiration time is set in seconds - 3930. You can modifiy the expiration time according to your needs.

var date = new Date();
date.setTime(date.getTime()+(3930*1000));
var expiry = '; expires=' + date.toUTCString();
document.cookie = 'abc=123'+expiry+'; path=/';

posted on 14.06.2019 at 14:08
0pnshow more
TimTim
share on facebookshare on twitter
2020 AnswerTabsTermsContact us
This site uses cookies to improve your experience, for analytics and ads. By continuing to browse this site you agree to our use of cookies.