### [微信小程序中倒计时的实现代码](https://www.prosaas.cn/article/17061) **Published:** 2018-09-06T02:26:27 **Author:** 5201314 **Excerpt:** 本篇文章给大家带来的内容是关于微信小程序中倒计时的实现代码,有一定的参考价值,有需要的朋友可以参考一下,希望对… 本篇文章给大家带来的内容是关于微信小程序中倒计时的实现代码,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。 在商城商品信息里,会有倒计时的功能。 ![](https://img.php.cn//upload/image/679/528/153/1536206227709360.png) 计算时间需要转化为时间戳,但是安卓和ios系统对于识别的时间格式是不一样的,安卓对识别没有要求。IOS格式要求 2018/08/20 10:20:32,使用Date.parse()转化时间戳不会出现在IOS端无法倒计时。 timeFormat: function(param) { //小于10的格式化函数 return param < 10 ? '0' + param : param; }, countDown: function() { //倒计时函数 // 获取当前时间,同时得到活动结束时间数组 let newTime = Date.parse(new Date()); let endTimeList = this.data.actEndTimeList; let countDownArr = \[\]; // 对结束时间进行处理渲染到页面 endTimeList.forEach(o => { var strtime = o.replace(/-/g, '/'); strtime = strtime.substring(0, 19); let endTime = new Date(strtime).getTime(); let obj = null; // 如果活动未结束,对时间进行处理 if (endTime - newTime > 0) { let time = (endTime - newTime) / 1000; // 获取天、时、分、秒 let day = parseInt(time / (60 \* 60 \* 24)); let hou = parseInt(time % (60 \* 60 \* 24) / 3600); let min = parseInt(time % (60 \* 60 \* 24) % 3600 / 60); let sec = parseInt(time % (60 \* 60 \* 24) % 3600 % 60); obj = { day: this.timeFormat(day), hou: this.timeFormat(hou), min: this.timeFormat(min), sec: this.timeFormat(sec) } } else { //活动已结束,全部设置为'00' obj = { day: '00', hou: '00', min: '00', sec: '00' } } countDownArr.push(obj); }) // 渲染,然后每隔一秒执行一次倒计时函数 this.setData({ countDownList: countDownArr }) setTimeout(this.countDown, 1000); }, 相关推荐: 基于JS实现的倒计时程序实例\_javascript技巧 jQuery团购倒计时特效实现方法\_jquery 以上就是微信小程序中倒计时的实现代码的详细内容,更多请关注知企PROSAAS其它相关文章! **Tags:** date, ios, jquery, JS, 微信小程序 **Categories:** 后端开发 ---