js 判断是否为第二天
-
以uni-app为例
-
首先我们要获取第二天0点的时间戳,存储到本地缓存中
-
然后我们进入app的时候判断当前的时间戳是否
>=上回记录的时间戳,如果大于,那么就是第二天了 -
天数不需要补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() }【注意】

549

被折叠的 条评论
为什么被折叠?



