Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which date formats can I use when specifying the expiry date when setting a 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.

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

like image 682
Dave Haigh Avatar asked Jun 21 '12 10:06

Dave Haigh


People also ask

How do you set the expiration date on cookies?

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.

What is the format for expiration date?

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”.

Which type of cookies is used to assign an expiration date?

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.

Which function in cookie is used to format date in standard format?

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

How to set the cookie expiry date of the cookie?

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.

What formatting do I have to match when creating cookies?

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.

How to set the weekday and the month in cookies?

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 do you read expiration dates on product codes?

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....


2 Answers

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; }; 
like image 158
Dave Haigh Avatar answered Sep 21 '22 18:09

Dave Haigh


The syntax specified in rfc 6265 for generating Set-Cookie headers uses
rfc1123-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.

like image 41
jfs Avatar answered Sep 20 '22 18:09

jfs