var defaultTimeStamp = Math.floor(Date.now()/1000);var dayA = defaultTimeStamp % (24 * 3600) //除去天数,得到剩余的小时时间戳var hourA = dayA % (3600) //除去小时,得到剩余的分钟数时间戳var minuteA = hourA % (60) //除去分钟,得到剩余的秒数时间戳var day = Math.floor(defaultTimeStamp / (24 * 3600));var hour = Math.floor(dayA / (3600));var minute = Math.floor(hourA / 60);var second = Math.floor(minuteA);
console.log(day, '天', hour, '时', minute, '分', second, '秒')
难点就是%百分号,取余数;
例:5%2 运算过程=》5除以2 商2 余1 所以5%2的结果为1;
所以我们只关心余数,dayA = defaultTimeStamp % (24 * 3600) 我们取的余数,得到还剩余多少小时的时间戳;
后面依此类推;
倒计时:
var ss = 60; var mm = 60;var hh = 24;var dd = 22;setInterval(function () {if (ss < 1) {ss = 60;if (mm < 1) {mm = 60if (hh < 1) {hh = 60dd--}hh--}mm--}ss--;that.setData({date: {dd: dd, hh: hh, mm: mm, ss: ss}})}, 1000)