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.
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.
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)?
Set an expiration date for a cookie This can be done easily by adding expires=expirationDate in UTC separated by semicolon from the name=value, as seen in the following example: document. cookie = "username=Max Brown; expires=Wed, 05 Aug 2020 23:00:00 UTC"; document.
In short, all financial transaction cards should show the card's expiration date in one of the following two formats: “MM / YY” or “MM-YY” — with the first being the by far most common for credit cards. This represents two digits for the month and two for the year — for example, “02 / 24”.
Persistent cookie Persistent cookies are not deleted by the browser when the user closes it. These cookies have an expiration date that you can set in your server.
Usage: setCookie("my-cookie-name","my-value","Sun, 15 Jul 2012 00:00:01 GMT");
In the SetCookie server side method, we are setting the cookie value and setting the Expiry date of the cookie by setting the Expires property. This will set the expiry date to current date time + 10 seconds, it means that this cookie should expire (the cookie should be deleted from the browser) in 10 seconds.
You have to match the following formatting: Both weekday and month have to be set in shorten format. To create this type of cookie, the whole date has to be correct. The weekday has to match the stated date.
Both weekday and month have to be set in shorten format. To create this type of cookie, the whole date has to be correct. The weekday has to match the stated date.
How to Read Expiration Dates 1 Read a closed code as a "made/manufactured on" date. 2 Read letters as though they were assigned to months. 3 Match an all-numerical code with a "month, day, year" sequence. 4 Interpret a 3-digit code as the date in a year that a product was made. See More....
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
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.
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; };
The syntax specified in rfc 6265 for generating Set-Cookie headers usesrfc1123-date = wkday "," SP date1 SP time SP "GMT"
cookie date format and therefore "Sun, 15 Jul 2012 00:00:01 GMT"
works.
If I understand it correctly, the parsing algorithm would recognize other formats e.g.: 00:00:01 15 jul 2012
but they should not be generated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With