今天替朋友写了个js 一个按钮 一天内只能点击一次(第二天凌晨cookies过期) 自己又从中学了不少知识
贴出代码:
<script>
function checkCookie()
{
var arrStr = document.cookie.split("; ");
if(arrStr[0]!="n=2"){
var exp =new Date(new Date()+86400000-(new Date().getHours()*60*60+new Date().getMinutes()*60+new Date().getSeconds())*1000);
exp.setTime(exp.getTime());
document.cookie = "n=2"+ ";expires=" + exp.toGMTString();
}else{
alert("您只能点击一次");
}
}
</script>
<input type=button id="btn" value="nihao"; οnclick="checkCookie()">
主要难点就是 第二天凌晨cookies过期
获取第二天凌晨的时间 :new Date(new Date()+86400000-(new Date().getHours()*60*60+new Date().getMinutes()*60+new Date().getSeconds())*1000);
也参考了许多网友的文章
得到前N天或后N天的日期
方法1:
<script type="text/javascript">
function showdate(n)
{
var uom = new Date(new Date()-0+n*86400000);
uom = uom.getFullYear() + "-" + (uom.getMonth()+1) + "-" + uom.getDate();
return uom;
}
window.alert("今天是:"+showdate(0));
window.alert("昨天是:"+showdate(-1));
window.alert("明天是:"+showdate(1));
window.alert("10天前是:"+showdate(-10));
window.alert("5天后是:"+showdate(5));
</script>
方法2:
<script type="text/javascript">
function showdate(n)
{
var uom = new Date();
uom.setDate(uom.getDate()+n);
uom = uom.getFullYear() + "-" + (uom.getMonth()+1) + "-" + uom.getDate();
return uom;
}
window.alert("今天是:"+showdate(0));
window.alert("昨天是:"+showdate(-1));
window.alert("明天是:"+showdate(1));
window.alert("10天前是:"+showdate(-10));
window.alert("5天后是:"+showdate(5));
</script>