7

I have this JavaScript code:

function spu_createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else
        var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

How can I make the cookie expire after 2 hours?

Dave
  • 44,275
  • 12
  • 65
  • 105
Tureac Cosmin
  • 83
  • 1
  • 1
  • 3
  • 1
    Maybe this bit of your code: `days*24*60*60*1000` will give you a clue about what is happening? Always a good idea to understand your own code, rather than rely on copy+paste programming. – Dave Sep 28 '13 at 16:20
  • Also don't put `var` inside ifs (especially if you use it after the if ends). It's bad style (and would be invalid in most other languages so you're setting yourself up for confusion later!) – Dave Sep 28 '13 at 16:25
  • Thank you all for answers I finally resolved my issue. I wanted to use it for a popup. And @Dave you're right, I reorganized a bit the code. – Tureac Cosmin Sep 28 '13 at 16:52

6 Answers6

10

If you want to use the same type of function, transform the days param into hours and pass 2 to get a 2 hour expiration date.

function spu_createCookie(name, value, hours)
{
    if (hours)
    {
        var date = new Date();
        date.setTime(date.getTime()+(hours*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else
    {
        var expires = "";
    }

    document.cookie = name+"="+value+expires+"; path=/";
}
Sébastien
  • 11,860
  • 11
  • 58
  • 78
5

Try this:

function writeCookie (key, value, hours) {
    var date = new Date();

    // Get milliseconds at current time plus number of hours*60 minutes*60 seconds* 1000 milliseconds
    date.setTime(+ date + (hours * 3600000)); //60 * 60 * 1000

    window.document.cookie = key + "=" + value + "; expires=" + date.toGMTString() + "; path=/";

    return value;
};

Usage:

<script>
writeCookie ("myCookie", "12345", 24);
</script>
//for 24 hours
VTodorov
  • 953
  • 1
  • 8
  • 25
OBV
  • 1,169
  • 1
  • 12
  • 25
5

Well -most obvious thing is to make "expire" date +2 hours ? :). Here You have nice prototype for that: Adding hours to Javascript Date object?

Community
  • 1
  • 1
masahuku
  • 184
  • 6
1

Try jquery-cookie. Makes it very easy to work with cookies.

Alex Art.
  • 8,711
  • 3
  • 29
  • 47
1

The following one-liner will set a cookie, name, with the value, value, and an expiration of two hours from the time of its creation. If the optional argument, days, is supplied, the cookie will expire after that many days instead.

Warning: there is no error-checking, so if mandatory parameters are omitted when called, or arguments are mistyped, the function will throw an error.

spu_createCookie = (name, value, days) => { document.cookie = `${name}=${value}; expires=${new Date(Date.now() + (days ? 86400000 * days : 7200000)).toGMTString()}; path=/` }

Relevant JavaScript syntax concepts:

  1. Arrow Functions

An arrow function expression is a compact alternative to a traditional function expression, but is limited and can't be used in all situations.

  1. Template Literals

Template literals are string literals allowing embedded expressions. You can use multi-line strings and string interpolation features with them.

  1. Ternary Operators

The conditional (ternary) operator is the only JavaScript operator that takes three operands ... This operator is frequently used as a shortcut for the if statement.

0

This would do it.

var now = new Date();
var time = now.getTime();
time += 7200 * 1000;
now.setTime(time);
document.cookie = 
     name+ '=' + value + 
     '; expires=' + now.toGMTString() + 
     '; path=/';
PMint
  • 246
  • 2
  • 13
  • "magic number on the wall…" better to use a more verbose `2*60*60*1000` for clarity, and rely on a minifying compiler to shorten it when you deploy. – Dave Sep 28 '13 at 16:28