wechatide://minicode/QUV0k2mG7R2Q
這是微信小程式的一個倒計時代碼片段,但在蘋果手機裡,這個倒計時并不會顯示!!
原因是在蘋果手機裡時間戳的轉換問題!!
可相容蘋果安卓手機的代碼,重點:
let arr = that.data.gmtDate.split(/[- :]/);// that.data.gmtDate時間格式為'2018-08-07 10:23:00'
let nndate = new Date(arr[0], arr[1] - 1, arr[2], arr[3], arr[4], arr[5]);
nndate=Date.parse(nndate)
let timeLeft = nndate- new Date();
以下 是相容蘋果安卓的js倒計時的微信小程式代碼片段!!!
wechatide://minicode/E7jtE8mk7M2Z
代碼詳解:
let goodsList = [{
actEndTime: '2019-05-01 10:00:43'
},
{
actEndTime: '2018-09-01 11:00:00'
},
{
actEndTime: '2018-06-01 12:45:56'
},
{
actEndTime: '2018-07-01 15:00:23'
},
{
actEndTime: '2018-11-23 17:00:22'
},
{
actEndTime: '2018-09-14 19:00:44'
},
{
actEndTime: '2018-10-21 21:00:34'
},
{
actEndTime: '2018-11-17 09:00:37'
},
{
actEndTime: '2018-12-21 05:00:59'
},
{
actEndTime: '2018-9-19 07:00:48'
},
{
actEndTime: '2018-10-28 03:00:11'
}
]
Page({
data: {
countDownList: [],
actEndTimeList: []
},
onLoad() {
let endTimeList = [];
// 将活動的結束時間參數提成一個單獨的數組,友善操作
goodsList.forEach(o => {
endTimeList.push(o.actEndTime)
})
this.setData({
actEndTimeList: endTimeList
});
// 執行倒計時函數
this.countDown();
},
timeFormat(param) { //小于10的格式化函數
return param < 10 ? '0' + param : param;
},
countDown() { //倒計時函數
// 擷取目前時間,同時得到活動結束時間數組
let newTime = new Date().getTime();
let endTimeList = this.data.actEndTimeList;
let countDownArr = [];
// 對結束時間進行處理渲染到頁面
endTimeList.forEach(o => {
///-----------------------------------------------------------------------------------------
//計算時間戳方式一,(ios有bug)
// let endTime = new Date(o).getTime();
//計算時間戳方式二,(相容ios和android)
let arr = o.split(/[- :]/);
let nndate = new Date(arr[0], arr[1] - 1, arr[2], arr[3], arr[4], arr[5]);
nndate = Date.parse(nndate)
let endTime = nndate;
///----------------------------------------------------------------------------------------------------------///
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);
}
})
<view class='tui-countdown-content' wx:for="{{countDownList}}" wx:key="countDownList">
剩餘
<text class='tui-conutdown-box'>{{item.day}}</text>天
<text class='tui-conutdown-box'>{{item.hou}}</text>時
<text class='tui-conutdown-box'>{{item.min}}</text>分
<text class='tui-conutdown-box tui-countdown-bg'>{{item.sec}}</text>秒
</view>
page{background-color: #eee;}
.tui-countdown-content{
height: 50px;
line-height: 50px;
text-align: center;
background-color: #fff;
margin-top: 15px;
padding: 0 15px;
font-size: 18px;
}
.tui-conutdown-box{
display: inline-block;
height: 26px;
width: 26px;
line-height: 26px;
text-align: center;
background-color: #000;
color: #fff;
margin: 0 5px;
}
.tui-countdown-bg{
background-color: #DF0101;
}