如何阅读cookie创建日期(未到期)

时间:2013-07-18 11:23:02

标签: javascript jquery cookies jquery-cookie

有没有办法在变量中存储cookie创建日期?我正在使用jquery.cookie插件。如果没有办法,我正考虑在cookie中存储,作为值,实际时间/日期。这可能是一个解决方案。

感谢。

2 个答案:

答案 0 :(得分:5)

您确实必须将时间存储在cookie本身中。浏览器的cookie API不会将创建日期作为元数据提供。

答案 1 :(得分:3)

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

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

//Create your cookie
HttpCookie yourCookie = new HttpCookie("Example");
//Add an actual value to the Values collection
yourCookie.Values.Add("YourValue", "ExampleValue");
//Add a Created Value to store the DateTime the Cookie was created
yourCookie.Values.Add("Created", DateTime.Now.ToString());
yourCookie.Expires = DateTime.Now.AddMinutes(30);

//Add the cookie to the collection
Request.Cookies.Add(yourCookie);

您可以通过以下网址访问您的页面:

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