如何将 JavaScript cookie 设置为在 10 年内过期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6348377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set a JavaScript cookie to expire in 10 years?
提问by Jake
The function I'm using sets the cookie expiration in this part of the code:
我使用的函数在这部分代码中设置了 cookie 过期时间:
time += 3600 * 1000;
It expires in an hour. How to set this to expire in 10 years?
一个小时后到期。如何将其设置为 10 年后到期?
回答by Dementic
Note: toGMTString() is deprecated since Jun 23, 2017, 9:04:01 AMand should no longer be used. It remains implemented only for backward compatibility; please use toUTCString() instead.
注意: toGMTString() 自2017年6 月 23 日上午 9:04:01起已弃用,不应再使用。它仍然只是为了向后兼容而实现;请改用 toUTCString()。
For more information: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toGMTString
更多信息: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:
您仍然可以仅仅通过更换达到同样的效果toGMTString()
与toUTCString()
像这样:
var CookieDate = new Date;
CookieDate.setFullYear(CookieDate.getFullYear() +10);
document.cookie = 'myCookie=to_be_deleted; expires=' + CookieDate.toUTCString() + ';';
回答by JAAulde
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;
回答by Derek
It will be
这将是
time += (3600 * 1000)*87660
8766 is the number of hours in year. The precise measurement is: 8 765.81277 hours
8766 是一年中的小时数。精确测量为:8 765.81277小时