2016-09-22 212 views
1

如何設置cookie在此日期過期:2038年1月19日?Cookie設置日期過期

如果我嘗試這樣的cookie不會設置:

$.cookie('test', true, {expires: "2038-01-19, 03:14:08 UTC"}); 
+0

檢查this..http://stackoverflow.com/questions/14939994/jquery-cookies-set-expire-time –

回答

0

我找到了解決辦法:

var date = new Date(); 
    date.setFullYear(2038); 
    $.cookie('test', true, {expires: date}); 
0

計算從當前日期到日期的天數你希望你的cookie到期,然後設置'天'值來設置cookie到期。

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; 
0

使用本

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()); 
}