cookie设置在特定时间点过期的方法

假设需求为:在当天晚上0:00过期。

方法:

得到当天晚上0:00这个时间点的一个时间。

 function getNextDate(){
        var d = new Date(),
            year = d.getFullYear(),
            month = d.getMonth(),
            day = d.getDate(),
            nextDay = new Date( year, month, ( day + 1 ), 00, 00, 00 );

            return nextDay;
    },

接下来是设置cookie:

$.cookie("someKey", "someVal", {expires : getNextDate()});

备注:之前一直以为cookie只能设置以天为单位的数字,现在看来,也可以设置一个date类型的对象

原文地址:https://www.cnblogs.com/charling/p/3449288.html