js获取当天剩余时间(戳)

function day_limit() {

var times = getCookie('times');

alert(times);

times = Math.round(times);

if(times > 60) {

//        console.log('今日观看时长:' + (times - 3600));

document.write('今日观看时间已到!');

return false;

}

expire = getExpire();

setCookie('times', times + 60, "s"+expire);

}

/****************************以下为cookie操作*****************************/

//使用示例

//setCookie("name","hayden");

//alert(getCookie("name"));

function setCookie(name,value,time) {

var strsec = getsec(time);

var exp = new Date();

exp.setTime(exp.getTime() + strsec*1);

document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();

}

function getsec(str) {

//  alert(str);

var str1=str.substring(1,str.length)*1;

var str2=str.substring(0,1);

if (str2 == "s") {

return str1*1000;

} else if (str2 == "h") {

return str1*60*60*1000;

} else if (str2 == "d") {

return str1*24*60*60*1000;

}

}

//读取cookies

function getCookie(name) {

var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");

if(arr=document.cookie.match(reg))

return unescape(arr[2]);

else

return null;

}

//删除cookies

function delCookie(name) {

var exp = new Date();

exp.setTime(exp.getTime() - 1);

var cval=getCookie(name);

if(cval!=null)

document.cookie= name + "="+cval+";expires="+exp.toGMTString();

}

//从当前时间到明日0点的时间戳

function getExpire() {

var today = new Date();

today.setHours(0);

today.setMinutes(0);

today.setSeconds(0);

today.setMilliseconds(0);

//明日0点时间戳

var tomorrow_0 = today.getTime()/1000+(24*3600);

var current_time = Math.round(new Date().getTime()/1000);

var expire = tomorrow_0 - current_time;

return expire;

}

你可能感兴趣的:(js获取当天剩余时间(戳))