2

How can I set the cookie to expire at this date: 19 Jan 2038 ?

The cookie is not set if I try like this:

$.cookie('test', true, {expires: "2038-01-19, 03:14:08 UTC"});
Valip
  • 4,440
  • 19
  • 79
  • 150

4 Answers4

0

I found the solution:

  var date = new Date();
  date.setFullYear(2038);
  $.cookie('test', true, {expires: date});
Valip
  • 4,440
  • 19
  • 79
  • 150
0

Calculate the number of days from current date to the Date you want your cookie to expire and then set that 'days' value to set the cookie expiry.

var start = new Date();
var end = new Date("19 Jan 2038");

// end - start returns difference in milliseconds 
var diff = new Date(end - start);

// get days
var days = diff/1000/60/60/24;
Ashish Mulaye
  • 256
  • 1
  • 3
  • 13
0

use this

document.cookie = "username=John Doe; expires=Tue, 19 Jan 2038 12:00:00 UTC";

function setUserConfigInCookie(){
  var expireDate = new Date;
  expireDate.setDate(expireDate.getDate() + 7); // It expires in a week

  var options = {
    path: '/',
    expiresAt: expireDate
  }

  userConfig = {}; // This is a valid JSON object that you need to save in a cookie
  $.cookies.set( 'userConfig', userConfig, options);

  // console.log('Expires=' + expireDate.toGMTString());
}
Dhaarani
  • 1,350
  • 1
  • 13
  • 23
0

You can't as this is a bug with the way browsers handle dates.

https://bugzilla.mozilla.org/show_bug.cgi?id=27070

Alexis Tyler
  • 1,394
  • 6
  • 30
  • 48