SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2007
    Posts
    130
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    How do i create a relative expire date for a cookie?

    I can't find any info on this. Please someone tell me.

  2. #2
    He's No Good To Me Dead silver trophybronze trophy stymiee's Avatar
    Join Date
    Feb 2003
    Location
    Slave I
    Posts
    23,423
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    What do you mean "relative"? You can set an expiration date but it has to be explicit meaning you have to set a concrete date.

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2007
    Posts
    130
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I think i already found a solution.

    Code:
    var now = new Date();
    var year = new Date(now.getTime() + 31536000000);
    var expiryDate = year.toUTCString();
    
    document.cookie = "poll=1; Expires=" + expiryDate + "; path=/";
    I got that from here http://www.howtocreate.co.uk/tutoria...script/cookies

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,807
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Simply set the date you want in a date field to start with. Foe example to set the expiry one month in the future:

    Code:
    var today = new Date();
    today.setMonth(today.getMonth()+1);
    document.cookie = cookie_name+ cookie_value + ',expires=' + today.toGMTString();
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •