Javascript 在设置 cookie 时指定到期日期时可以使用哪些日期格式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11136372/
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
Which date formats can I use when specifying the expiry date when setting a cookie?
提问by Dave Haigh
I am using a function which sets a cookie. This function allows the cookie name, the cookie value and an additional expiry date of the cookie to be passed into it.
我正在使用一个设置 cookie 的函数。此函数允许将 cookie 名称、cookie 值和 cookie 的附加到期日期传递给它。
the function:
功能:
function setCookie(name, value, exdate) {
var c_value = escape(value) + ((exdate === null || exdate === undefined) ? "" : "; expires=" + exdate);
document.cookie = name + "=" + c_value;
};
usage:
用法:
setCookie("my-cookie-name","my-value","Sun, 15 Jul 2012 00:00:01 GMT");
I have used the function with the date format above and believe it is cross browser compatible as I have tested if the cookie remains after closing various browsers and reopening them. I discovered that there were problems when using a format like "15 Jul 2012"
. This format worked for me during development in firefox, but other browsers only seemed to set the cookie as a session cookie.
我已经使用了上述日期格式的函数,并且相信它是跨浏览器兼容的,因为我已经测试过在关闭各种浏览器并重新打开它们后 cookie 是否仍然存在。我发现使用像"15 Jul 2012"
. 这种格式在 firefox 的开发过程中对我有用,但其他浏览器似乎只将 cookie 设置为会话 cookie。
Should I stick to using just this format: "Sun, 15 Jul 2012 00:00:01 GMT" or are there other formats I could use for the expiry date that will work across the major browsers (IE 7-9, Firefox, Chrome, Opera, Safari)?
我应该坚持只使用这种格式:“Sun, 15 Jul 2012 00:00:01 GMT” 还是我可以使用其他格式来确定适用于主要浏览器(IE 7-9、Firefox、Chrome)的到期日期、Opera、Safari)?
EDIT/UPDATE:
编辑/更新:
Cookies require the expiry date to be in UTC/GMT Format(see answer below).
Cookie 要求有效期为UTC/GMT 格式(请参阅下面的答案)。
I have edited my function to the following in order to convert any dates passed in that are not in the corect format.
我已将我的函数编辑为以下内容,以便转换传入的任何非正确格式的日期。
function setCookie(name, value, exdate) {
//If exdate exists then pass it as a new Date and convert to UTC format
(exdate) && (exdate = new Date(exdate).toUTCString());
var c_value = escape(value) + ((exdate === null || exdate === undefined) ? "" : "; expires=" + exdate);
document.cookie = name + "=" + c_value;
};
采纳答案by Dave Haigh
Based on testing and further reading into this, a date in a UTC/GMT format is required by cookiese.g. Sun, 15 Jul 2012 00:00:01 GMT
根据测试和进一步阅读,cookie 需要 UTC/GMT 格式的日期,例如Sun, 15 Jul 2012 00:00:01 GMT
Therefore any dates in other formats such as 15 Jul 2012, or 15/Jul/2012, or 07/15/2012, have to be passed as a new Date
object and then through the toUTCString()
or the toGMTString()
function.
因此,任何其他格式的日期,例如15 Jul 2012,或15/Jul/2012,或07/15/2012,都必须作为new Date
对象传递,然后通过toUTCString()
或toGMTString()
函数。
therefore I have edited my function to the following:
因此,我已将我的功能编辑为以下内容:
function setCookie(name, value, exdate) {
//If exdate exists then pass it as a new Date and convert to UTC format
(exdate) && (exdate = new Date(exdate).toUTCString());
var c_value = escape(value) + ((exdate === null || exdate === undefined) ? "" : "; expires=" + exdate);
document.cookie = name + "=" + c_value;
};
回答by jfs
The syntax specified in rfc 6265 for generating Set-Cookie headersusesrfc1123-date = wkday "," SP date1 SP time SP "GMT"
cookie date format and therefore "Sun, 15 Jul 2012 00:00:01 GMT"
works.
rfc 6265 中指定的用于生成 Set-Cookie 标头的语法使用rfc1123-date = wkday "," SP date1 SP time SP "GMT"
cookie 日期格式,因此"Sun, 15 Jul 2012 00:00:01 GMT"
有效。
If I understand it correctly, the parsing algorithmwould recognize other formats e.g.: 00:00:01 15 jul 2012
but they should not be generated.
如果我理解正确,解析算法会识别其他格式,例如: 00:00:01 15 jul 2012
但不应生成它们。