How to set a JavaScript cookie to expire in 10 years?
The function I'm using sets the cookie expiration in this part of the code:
ti开发者_如何学JAVAme += 3600 * 1000;
It expires in an hour. How to set this to expire in 10 years?
Note: toGMTString() is deprecated since Jun 23, 2017, 9:04:01 AM and should no longer be used. It remains implemented only for backward compatibility; please use toUTCString() instead.
For more information: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toGMTString
var CookieDate = new Date;
CookieDate.setFullYear(CookieDate.getFullYear() +10);
document.cookie = 'myCookie=to_be_deleted; expires=' + CookieDate.toGMTString() + ';';
You can still achieve the same result just by replacing toGMTString()
with toUTCString()
as so:
var CookieDate = new Date;
CookieDate.setFullYear(CookieDate.getFullYear() +10);
document.cookie = 'myCookie=to_be_deleted; expires=' + CookieDate.toUTCString() + ';';
Well, how many:
- hours are in a day?
- days are in a year?
- years are in a decade?
Answer:
time += 3600 * 1000 * 24 * 365 * 10;
It will be
time += (3600 * 1000)*87660
8766 is the number of hours in year. The precise measurement is: 8 765.81277 hours
精彩评论