开发者

YUI set cookies expire time

开发者 https://www.devze.com 2023-02-15 15:55 出处:网络
I am working on in YUI framework with my client. I have a difficult to set a cookies expire by following YUI syntax.

I am working on in YUI framework with my client. I have a difficult to set a cookies expire by following YUI syntax.

According to the example, in here: http://developer.yahoo.com/yui/cookie/#creatingcookies

I can create a cookies by setting up expire date. However, I tried to set the cookies will expire in 4 hours. It doesnot create a cookies at all.

var output = Y.DataType.Date.format(new Date(), {format:"%H"}); //Show the current hours
var expireTime= output+4; // 4 hours later, the cookies开发者_如何学C will expire

YAHOO.util.Cookie.set("name", "value", {
    expires: new Date(expireTime)
});

Does anyone know what I did wrong in here?

Cheers, Qing


To get a Date instance 4 hours in the future:

var d = new Date();
d.setHours(d.getHours() + 4);

You can then use "d" as the "expires" value.

Your code asks for a formatted Date, which means you're asking for a string. Thus the resulting value of your "expireTime" variable will be a string consisting of the hours digits from the time, with a "4" character appended. So if it were 10 o'clock, you'd get "104" for "expireTime".

0

精彩评论

暂无评论...
验证码 换一张
取 消