javascript – 如何读取cookie创建日期(未到期)

前端之家收集整理的这篇文章主要介绍了javascript – 如何读取cookie创建日期(未到期)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有没有办法在变量中存储cookie创建日期?我正在使用 jquery.cookie插件.如果没有办法,我正考虑在cookie中存储,作为值,实际时间/日期.这可能是一个解决方案.

谢谢.

解决方法

  1. <!-- Output the DateTime that the cookie is set to expire -->
  2. @Request.Cookies["YourCookie"].Expires.ToString()

但是,我不相信有一个属性可以获得创建日期,除非您专门将值本身存储为Cookie本身的附加值:

  1. //Create your cookie
  2. HttpCookie yourCookie = new HttpCookie("Example");
  3. //Add an actual value to the Values collection
  4. yourCookie.Values.Add("YourValue","ExampleValue");
  5. //Add a Created Value to store the DateTime the Cookie was created
  6. yourCookie.Values.Add("Created",DateTime.Now.ToString());
  7. yourCookie.Expires = DateTime.Now.AddMinutes(30);
  8.  
  9. //Add the cookie to the collection
  10. Request.Cookies.Add(yourCookie);

您可以在页面中访问:

  1. Created : @Request.Cookies["Example"].Values["Created"].ToString()
  2. Expires : @Request.Cookies["Example"].Expires.ToString()

猜你在找的JavaScript相关文章