step by step.
目录
一、 js
1、 js文件中添加变量dates。
dates:[],
2、 onLoad函数中添加获取当前时间、目标时间的函数
var that = this;
setInterval(function(){
var kaoyan_time = new Date("2022/12/25 9:00:00") //目标时间:考研时间
var current_time = new Date(); //当前时间:无参构造Date
var days = new Date(kaoyan_time - current_time + 16*3600*1000) //计算天数
that.setData({
dates: parseInt(days.getTime()/1000/3600/24) //转换为Int
})
},1000)
3、 setInterval
4、 onLoad
二、 wxml引用方法
用 {{dates}} 引用
<text class="days" style="position: relative; left: -10rpx; top: -286rpx">距离考研还有{{dates}}天</text>