Set cookie to expire after fixed date, not x amount of time

Posted on 2012-08-12
Last Modified: 2012-08-12

I'm trying to set a cookie to expire every morning at 9am.
How should i do this (with jQuery)?

Set, read, delete is all working fine.
This sets the cookie to expire after 1 day, i want it to expire next day @ 9am.
$.cookie("newsq", "1", { path: '/', expires: 1 });

is this possible?
Question by:peps03
    LVL 49

    Expert Comment

    by:Julian Hansen
    When you set the cookie work out how long between time you are setting the cookie and 9am - convert that to seconds and set the cookie.

    Author Comment

    I've been trying to get it to work this way, since your post.
    I'm not managing.

    Could you maybe help me with the code?

    the cookie is set to 0 by default.
    once a button is clicked, it is set to 1. until it is supposed to expire.
    LVL 49

    Accepted Solution

    How about this?
    function setCookie(value)
      // Get date and time now
      var cur = new Date();
      // Add a day 86,400 secs * 1000 ms
      var tn = new Date(cur.getTime() + 86400000);
      // Create a new date with tomorrows Year / Month and day but 9 in the morning
      var et = new Date(tn.getFullYear(), tn.getMonth(), tn.getDate(), 9,0);
      // Finally create your cookie outputing new date as a UTCString
      document.cookie='mycookie=' + escape(value) + '; expires=' + et.toUTCString();

    Open in new window


    Author Comment

    thanks. for testing purposes, how would i set it just minutes in the future?
    LVL 49

    Assisted Solution

    by:Julian Hansen
    Something like this ...

    var dt = new Date();
    var min = 10; // change to whatever you want
    var nd = new Date(dt.getTime() + (min * 60 * 1000));

    Open in new window


    Author Closing Comment

    Thanks!!! It works like a charm! :)
    LVL 49

    Expert Comment

    by:Julian Hansen

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
    This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
    In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
    The viewer will learn how to dynamically set the form action using jQuery.

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now