How to get cookie expiration date / creation date from javascript?

JavascriptCookies

Javascript Problem Overview


Is it possible to retrieve the creation or expiration date of an existing cookie from javascript? If so how?

Javascript Solutions


Solution 1 - Javascript

It's impossible. document.cookie contains information in string like this:

key1=value1;key2=value2;...

So there isn't any information about dates.

You can store these dates in separate cookie variable:

auth_user=Riateche;auth_expire=01/01/2012

But user can change this variable.

Solution 2 - Javascript

The information is not available through document.cookie, but if you're really desperate for it, you could try performing a request through the XmlHttpRequest object to the current page and access the cookie header using getResponseHeader().

Solution 3 - Javascript

It's now possible with new chrome update for version 47 for 2016 , you can see it through developer tools on Storage > Cookies under the Application tab enter image description here, select cookies and look for your cookie expiration date under "Expires/Max-age"

Solution 4 - Javascript

you can't get the expiration date of a cookie through javascript because when you try to read the cookie from javascript the document.cookie return just the name and the value of the cookie as pairs

Solution 5 - Javascript

Yes, It is possible. I've separated the code in two files:

index.php

<?php
	
	$time = time()+(60*60*24*10);
	$timeMemo = (string)$time;
	setcookie("cookie", "" . $timeMemo . "", $time);
	
?>
<html>
	<head>
		<title>
			Get cookie expiration date from JS
		</title>
		<script type="text/javascript">
			
			function cookieExpirationDate(){
				
				var infodiv = document.getElementById("info");
				
				var xmlhttp;
				if (window.XMLHttpRequest){ 
					xmlhttp = new XMLHttpRequest;
				}else{
					xmlhttp = new ActiveXObject(Microsoft.XMLHTTP);
				}
				
				xmlhttp.onreadystatechange = function (){
					if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
						infodiv.innerHTML = xmlhttp.responseText;
					}
				}
				
				xmlhttp.open("GET", "cookie.php", true);
				xmlhttp.send();
				
			}
			
		</script>
	</head>
	<body>
		<input type="button" onclick="javascript:cookieExpirationDate();" value="Get Cookie expire date" />
		<hr />
		<div id="info">
		</div>
	</body>
</html>

cookie.php

<?php

	function secToDays($sec){
		return ($sec / 60 / 60 / 24);
	}

	if(isset($_COOKIE['cookie'])){

		if(round(secToDays((intval($_COOKIE['cookie']) - time())),1) < 1){
			echo "Cookie will expire today";
		}else{
			echo "Cookie will expire in " . round(secToDays((intval($_COOKIE['cookie']) - time())),1) . " day(s)";
		}
	}else{
		echo "Cookie not set...";
	}

?>

Now, index.php must be loaded once. The button "Get Cookie expire date", thru an AJAX request, will always get you an updated "time left" for cookie expiration, in this case in days.

Solution 6 - Javascript

One possibility is to delete to cookie you are looking for the expiration date from and rewrite it. Then you'll know the expiration date.

Solution 7 - Javascript

If you are using Chrome you can goto the "Resources" tab 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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNirView Question on Stackoverflow
Solution 1 - JavascriptPavel StrakhovView Answer on Stackoverflow
Solution 2 - JavascriptVictor WellingView Answer on Stackoverflow
Solution 3 - JavascriptYazan RawashdehView Answer on Stackoverflow
Solution 4 - Javascriptbassem alaView Answer on Stackoverflow
Solution 5 - JavascriptPedro SousaView Answer on Stackoverflow
Solution 6 - JavascriptinorganikView Answer on Stackoverflow
Solution 7 - Javascriptskribbz14View Answer on Stackoverflow