JS判断当前日期必须大于选择日期

版权声明:作者:星云 交流即分享,分享才能进步!喜欢我的文章,可在博客左侧扫码赞赏~ https://blog.csdn.net/hadues/article/details/89334944

有时候我们需要JS校验判断当前日期必须大于选择日期,这个怎么实现呢?

解决方案:

封装测试方法如下:

<script>
         if(judgeDate(modifyDate)<0){
            alert('恢复日期必须小于当天日期');
            return ;
        }
function judgeDate(tomodifyDate){
    return new Date().getTime()-new Date(tomodifyDate).getTime();
}
</script>

实现思路:
1.假设修改日期格式是一个字符串 “2019-04-16”
2.获取当前的时间戳 new Date().getTime()
3.获取要修改的时间戳 new Date("2019-04-16").getTime()
4. 当前时间戳减去要修改的时间戳

  • 如果是负数,说明修改日期大于了当前日期
  • 如果是正数,说明修改日期小于等于当前日期

JS 调试验证:
F12 然后找到Console 界面,输入以下代码,回车:

new Date().getTime()-new Date("2019-04-16").getTime();

猜你喜欢

转载自blog.csdn.net/hadues/article/details/89334944