Reading cookie expiration date
I agree with @Daniel, as he answered this in 2009.
But right now, I came across a similar problem and I found a better way to read the expiry date of cookie
in Javascript.
The CookieStore type of the cookies API represents a cookie store in the browser.
YOU CAN NOT USE THIS FOR NON HTTPS SITES
This is compatible with all modern browsers.
All you need is to
// list of all the cookies
cookieStore.getAll().then(cookies=>console.log(cookies))
// returns object of the single cookie by NAME
cookieStore.get('NAME_OF_THE_COOKIE').then(cookies=>console.log(cookies))
The output of CookieStore will be Promise, so you will need to resolve it. after that result array of cookies in the following format.
domain: null
expires: 1615699665000 //TIMESTAMP
name: "STR"
path: "/"
sameSite: "STR"
secure: "BOOL"
value: "STR"
Please feel free to correct me or update my answer for the better help of others.
There are some who will find this contentious - but one solution is to create another cookie at the same time and store the time/stamp in it parallel with whichever original cookie is created. Update them both at the same time and that way you can easily get the time from your new cookie (Alternatively append the time/stamp in your source cookie).
The reason this would be contentious is that over the years the idea of storing cookies on a users PC isn't popular because you are taking up their space. However I really doubt a small timestamp cookie would be too horrific.
Its worth remembering that if a time has passed then the browser will not report that cookie available. The browser may show the cookie present but when JS tries to access it - it won't be able too.
Additionally I found that WebDeveloper toolbar in Firefox shows cookies that have passed but under Firefox > Privacy settings they are updated correctly.
It is not possible to get the expiration date of a cookie through Javascript; only key-value pairs are exposed through document.cookie
.