微信小程序毫秒转天时分秒(有效果图)

效果图

请添加图片描述

.wxml

<view wx:if="{{time.deadline > 0}}">
  <view>单处理-转天时分秒
    <view>{{time.down}}</view>
  </view>
  <view>单处理-转时分秒
    <view>{{time.huly}}</view>
  </view>
</view>
<view wx:for="{{array}}" wx:key="{{index}}" wx:if="{{item.deadline > 0}}">
  <view>多处理-转天时分秒
    <view>{{item.down}}</view>
  </view>
  <view>多处理-转时分秒
    <view>{{item.huly}}</view>
  </view>
</view>

.wxss

view{box-sizing: border-box;}
page>view{font-size: 26rpx;padding:40rpx 30rpx;display: flex;justify-content: space-between;line-height: 44rpx;
border-bottom: 1px solid #f4f4f4;}
page>view:nth-child(1){border-bottom: 1px solid #FFCCCC;}

.js

Page({
  data: {
    time:{deadline: "1289602000"},
    array:[
      {deadline: "1289602000"},
      {deadline: "328960200"},
      {deadline: "289602000"},
    ]
  },
  onUnload(e){
    clearInterval(this.data.tiem_one)
    clearInterval(this.data.tiem_two)
  },
  onHide(e){
    clearInterval(this.data.tiem_one)
    clearInterval(this.data.tiem_two)
  },
  onLoad(options){
    this.single()
    this.multiple()
  },
  single(e){//单处理
    var that = this
    var time = that.data.time,days,hours,minutes,seconds,hours_s
    that.data.tiem_one = setInterval(function() {
      time.deadline = time.deadline - 1000
      days = parseInt(time.deadline / (1000 * 60 * 60 * 24));
      hours = parseInt((time.deadline % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
      minutes = parseInt((time.deadline % (1000 * 60 * 60)) / (1000 * 60));
      seconds = parseInt((time.deadline % (1000 * 60)) / 1000);
      if (time.deadline < 1) {
        that.setData({
          time:time
        })
        clearInterval(that.data.tiem_one)
        return false
      }
      hours_s = (hours + days * 24) > 9 ? (hours + days * 24) : '0' + (hours + days * 24)
      hours = hours > 9 ? hours : '0' + hours
      minutes = minutes > 9 ? minutes : '0' + minutes
      seconds = seconds > 9 ? seconds : '0' + seconds
      time.down = days + '天' + hours + '时' + minutes + '分' + seconds + '秒' 
      time.huly = hours_s + '时' + minutes + '分' + seconds + '秒' 
      that.setData({
        time:time
      })
    },1000)
  },
  multiple(e){//多处理
    var that = this
    var array = that.data.array,days,hours,minutes,seconds,hours_s
    that.data.tiem_two = setInterval(function() {
      array.forEach((item,index) => {
        if(item.deadline > 0){
          item.deadline = item.deadline - 1000
          days = parseInt(item.deadline / (1000 * 60 * 60 * 24));
          hours = parseInt((item.deadline % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
          minutes = parseInt((item.deadline % (1000 * 60 * 60)) / (1000 * 60));
          seconds = parseInt((item.deadline % (1000 * 60)) / 1000);
          hours_s = (hours + days * 24) > 9 ? (hours + days * 24) : '0' + (hours + days * 24)
          hours = hours > 9 ? hours : '0' + hours
          minutes = minutes > 9 ? minutes : '0' + minutes
          seconds = seconds > 9 ? seconds : '0' + seconds
          item.down = days + '天' + hours + '时' + minutes + '分' + seconds + '秒' 
          item.huly = hours_s + '时' + minutes + '分' + seconds + '秒' 
        }
      })
      that.setData({
        array:array
      })
    },1000)
  },
})

遇到问题可以看我主页加我Q,很少看博客,对你有帮助别忘记点赞收藏。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值