Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading cookie expiration date

Is it possible to read cookie expiration date using JavaScript?

If yes, how? If not, is there a source I can look at?

like image 850
DarthVader Avatar asked Oct 07 '09 15:10

DarthVader


People also ask

Can I eat cookie after expiration date?

When cookies or chips get old, the stale taste is quite obvious. But as long as it doesn't smell funky (the oils in the cookie may go bad over a long period of time) and it doesn't crumble apart in your hand, then it's okay to eat.

Is the sell by date the expiration date on cookies?

Remember that cookies, like a lot of other sweets, usually have a best by date and not an expiration date. Because of this distinction, you may safely use them to satisfy your sweet tooth even after the best before date has lapsed.

Which cookie has 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.

How do you set the expiration date to a cookie explain with example?

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.


6 Answers

It is not possible to get the expiration date of a cookie through Javascript; only key-value pairs are exposed through document.cookie.

like image 114
Daniel Vandersluis Avatar answered Sep 24 '22 23:09

Daniel Vandersluis


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.

like image 31
Antony Avatar answered Sep 25 '22 23:09

Antony


If you are using Chrome you can goto the "Application" tab (within Developer Tools) and find the item "Cookies" in the left sidebar. From there select the domain you are checking the set cookie for and it will give you a list of cookies associated with that domain, along with their expiration date.

like image 42
skribbz14 Avatar answered Sep 24 '22 23:09

skribbz14


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.

like image 36
Gautam Jha Avatar answered Sep 24 '22 23:09

Gautam Jha


You have several options.

  1. Save expire timestamp somewhere else - e.g. localStorage or another Cookie
  2. In Chrome and Edge - use CookieStore API which i describe below

You can use CookieStore API which is "experimental" as I write, yet it's supported by Chrome. Can't test, but seems like on Edge too. Tested on Safari, it's undefined, so not supported.

Yet you can check here: https://caniuse.com/?search=CookieStore
Read more here: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/cookies/CookieStore

On Chrome sample response:

enter image description here

Safari 15.1 (2021-11-13):

enter image description here

like image 26
Lukas Liesis Avatar answered Sep 25 '22 23:09

Lukas Liesis


If you have control over the code where the cookie is set, then you can add code to store the expiration date in the browser's local storage for later retrival. For example:

// set cookie with key and value to expire 30 days from now
var expires = new Date(Date.now() + 30 * 24 * 60 * 60 * 1000);
document.cookie = [
    "key=value",
    "expires=" + expires.toUTCString()
].join("; ");

// store cookie expiration date for key in local storage
if (window.localStorage) {
    localStorage.setItem("keyExpires", expires.toISOString());
}

Later, you can lookup when the cookie expires in local storage. For example:

var expires = localStorage.getItem("keyExpires");

Local storage currently has broad support.

https://caniuse.com/#feat=namevalue-storage

like image 30
Keith Shaw Avatar answered Sep 25 '22 23:09

Keith Shaw