santro
santro

Reputation: 67

Set Expires Time in Cookie Javascript

I have a string that I use to set expires date in cookie. But I always fail to set it. Here is my Code:

var expTime = '2016-06-09T03:06:53Z';
var valueCookie = 'test'; 
SetCookie('myCookie', valueCookie, expTime);


function SetCookie(name, value, expTime) {
document.cookie = name + '=' + value + '; ' 'expires=' + expTime+ '; path=/';

};

Why I fail to set Expire Date in Cookie?

Thanks

Upvotes: 0

Views: 2014

Answers (3)

Hameed Syed
Hameed Syed

Reputation: 4285

I think there is a syntax error.Try this

var expTime = '2016-06-09T03:06:53Z';
var valueCookie = 'test'; 
SetCookie('myCookie', valueCookie, expTime);


function SetCookie(name, value, expTime) {
document.cookie = name + '=' + value + '; expires=' + expTime+ '; path=/';
}

Upvotes: 1

Tang Jia
Tang Jia

Reputation: 11

here is my code

function setCookie(name,value,data){
    var oDate =new Date();
    oDate.setDate(oDate.getDate()+data);
    document.cookie=name+'='+value+';expires='+oDate;
}

Upvotes: 1

Fadhly Permata
Fadhly Permata

Reputation: 1686

try this

function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires="+ d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
} 

The parameters of the function above are the name of the cookie (cname), the value of the cookie (cvalue), and the number of days until the cookie should expire (exdays).

The function sets a cookie by adding together the cookiename, the cookie value, and the expires string.

Upvotes: 1

Related Questions