在设置Cookie时指定到期日期时,我可以使用哪些日期格式? [英] Which date formats can I use when specifying the expiry date when setting a cookie?

查看:159
本文介绍了在设置Cookie时指定到期日期时,我可以使用哪些日期格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个设置cookie的函数。此函数允许Cookie名称,Cookie值和Cookie的附加到期日期传递到该函数中。

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.

函数 / p>

the function:

function setCookie(name, value, exdate) {
    var c_value = escape(value) + ((exdate === null || exdate === undefined) ? "" : "; expires=" + exdate);
    document.cookie = name + "=" + c_value;
};

用法:

setCookie("my-cookie-name","my-value","Sun, 15 Jul 2012 00:00:01 GMT");

我使用的函数与上面的日期格式,并相信它是跨浏览器兼容,因为我已经测试如果Cookie在关闭各种浏览器并重新打开之后仍然存在。我发现使用格式2012年7月15日时出现问题。这种格式适用于我在Firefox开发期间,但其他浏览器只是似乎将cookie设置为会话cookie。

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.

我应该坚持使用这种格式:Sun,2012年7月15日00:00:01 GMT或者有其他格式,我可以使用的过期日期,将工作在主要的浏览器(IE 7-9,Firefox,Chrome,Opera,Safari)?

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)?

编辑/更新:

Cookie需要到期日在 UTC / GMT格式(请参阅下面的答案)。

Cookies require the expiry date to be in UTC/GMT Format (see answer below).

我已将我的函数编辑为以下转换不是corect格式。

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;
    };


推荐答案

根据测试和进一步阅读,在 UTC / GMT格式需要Cookie ,例如 Sun,15 Jul 2012 00:00:01 GMT

Based on testing and further reading into this, a date in a UTC/GMT format is required by cookies e.g. Sun, 15 Jul 2012 00:00:01 GMT

因此,任何其他格式的日期,例如 2012年7月15日,或 15/2012年7月 07/15/2012 ,必须以新日期对象,然后通过 toUTCString() toGMTString()函数。

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.

因此我已将我的函数编辑为以下内容:

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;
};

这篇关于在设置Cookie时指定到期日期时,我可以使用哪些日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆