js 判断是否为第二天

js 判断是否为第二天

  1. 以uni-app为例

  2. 首先我们要获取第二天0点的时间戳,存储到本地缓存中

  3. 然后我们进入app的时候判断当前的时间戳是否 >= 上回记录的时间戳,如果大于,那么就是第二天了

  4. 天数不需要补0

    methods:{
        //获取第二天0点的时间戳
    	twoDay(){
            let timeObj = {}
            let date = new Date();
            let year = date.getFullYear();
            let month = date.getMonth() + 1;
            let day = date.getDate();
            let monthDay = this.getDay(year, month)//获取今年这个月的天数
            let newTime = ''
            if (day < monthDay) {
           		newTime = `${year}-${month}-${day+1}`; //注意不需要补0,要不然是第二天八点
        	} else {
           		if (month < 12) {
             		newTime = `${year}-${month+1}-1`;
          		} else {
             		newTime = `${year+1}-1-1`
           		}
        	}
            timeObj.twoTimeMs = new Date(newTime).getTime()//第二天0点毫秒值;
            timeObj.signIn = false;//默认没签到
            uni.setStorageSync('timeObj', timeObj);//uni-app的api(存储到本地缓存中)
            return timeObj;
    	}
    
        getDay(year, month) {
            return new Date(year, month, 0).getDate()
        }
        getStorageTimeObj() {
             let timeObj;
             try {
                 timeObj = uni.getStorageSync('timeObj');//获取前先从本地获取,获取失败(进入catch)即用户第一次下载安装app或者手动清除了缓存
             } catch (e) {
                 timeObj = this.twoDay()
             }
             let date = new Date()
             console.log(timeObj);
             if (timeObj.twoTimeMs <= date.getTime()) {
                 //第二天了,做第二天的事情,如调用签到的函数,是否调用签到取决于timeObj.signIn是否为false,签到后让timeObj.signIn = true,再将timeObj存储到本地即可。
                 .....
                 this.twoDay();//重新更新时间戳
             }
    
         }
    },
    onLaunch:function(){
        this.getStorageTimeObj()
    }
    

    【注意】
    在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值