开发者

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:

  1. hours are in a day?
  2. days are in a year?
  3. 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜