Javascript and dynamic pages cannot get the time when the cookie expires. The expiration time is managed by the browser. Javascript and dynamic pages can only set the expiration time and cannot be obtained through the document.cookie (javascript) or Cookie.Expires (asp.net) attributes.
The code copy is as follows:
<%@page language="C#" Debug="true"%>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
HttpCookie hc = Request.Cookies["abc"];
if (hc != null)
{
Response.Write(hc.Expires);//0001-1-1 0:00:00
Response.End();
}
}
</script>
Although the cookie of asp.net has an Expires attribute, the Expires attribute output of Response.Write gets 0001-1-1 0:00:00 (DateTime.MinValue). This is because the browser does not send the expiration time of the cookie to the server, so use DateTime.MinValue to fill in the Expires attribute of the cookie.
You must obtain the expiration time, and you need to record the expiration time of the corresponding cookie through another cookie value. as follows:
The code copy is as follows:
<script>
var d = new Date();
d.setHours(d.getHours() + 1); //1 hour expires
document.cookie = 'testvalue=123;expires=' + d.toGMTString(); //Storage cookie value
document.cookie = 'testexp=' + escape(d.toLocaleString()) + ';expires=' + d.toGMTString(); //Storage the cookie expiration time. To get the expiration time of the testvalue cookie, you can achieve it by getting the testexp cookie
</script>