js 操作时间 获取第二天凌晨时间

今天替朋友写了个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>

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值