如何读取cookie创建日期(不是过期)

问题描述 投票:0回答:3

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

谢谢。

javascript jquery cookies jquery-cookie
3个回答
6
投票

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


4
投票
<!-- 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()

0
投票

如果您知道cookie的过期时间。您可以从完整的过期时间中减去 cookie 上剩余的时间。这是你必须回到过去才能知道创建日期的时间。

© www.soinside.com 2019 - 2024. All rights reserved.