How to read the cookie creation date (not expiration)
Asked Answered
S

3

9

Is there a way to store in a variable a cookie creation date? I'm using the jquery.cookie plugin. If there is not a way, I'm thinking about store in the cookie, as value, the actual time/date. It could be a solution.

Thanks.

Serviceberry answered 18/7, 2013 at 11:23 Comment(5)
Could you not assume that the cookie expiration date is x time after set time? So you could just simply get it by (expiration date) - (x time)?Glint
@Puuskis, good thought, but how will get the 'x time', that is lacking in this question. can you please comment on this if you have any answer.Occasionally
@SAM If you are setting the expiration date yourself, you could use the same time to calculate it. For example if all cookies expire 7 days after, you just reduce 7 days from the date. If you don't want them to expire, set them so far in future it is not relevant but you can still count the time from it.Glint
It is a good solution too.Serviceberry
If you click on lock icon previous to url in chromium browser, there select cookie, & select any among the stored cookie, you will see 'created' attribute value is given with when it's created. Not sure if here browser explicitly store when cookies getting addedHopfinger
O
4
<!-- Output the DateTime that the cookie is set to expire -->
@Request.Cookies["YourCookie"].Expires.ToString()

However, I don't believe that there is a property to get the Creation Date, unless you were to specifically store the value itself as an additional value within the Cookie itself :

//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);

which you could access in your page through :

Created : @Request.Cookies["Example"].Values["Created"].ToString()
Expires : @Request.Cookies["Example"].Expires.ToString()
Odontalgia answered 18/7, 2013 at 11:26 Comment(0)
C
6

You will indeed have to store the time in the cookie itself. The browser's cookie API does not supply the creation date as metadata.

Clientele answered 18/7, 2013 at 11:24 Comment(1)
Thanks for the answer. That was as I thought.Serviceberry
O
4
<!-- Output the DateTime that the cookie is set to expire -->
@Request.Cookies["YourCookie"].Expires.ToString()

However, I don't believe that there is a property to get the Creation Date, unless you were to specifically store the value itself as an additional value within the Cookie itself :

//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);

which you could access in your page through :

Created : @Request.Cookies["Example"].Values["Created"].ToString()
Expires : @Request.Cookies["Example"].Expires.ToString()
Odontalgia answered 18/7, 2013 at 11:26 Comment(0)
D
0

If you know the expiration time of the cookie. You could subtract the time left on the cookie from the full expiration time. This is the time you have to go back in the past to know the creation date.

Deathtrap answered 24/9, 2024 at 19:56 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.