微信小程序时间戳转时间,时间转时间戳,活动倒计时,清除定时器

1.时间转时间戳

  dataTime(){
    //获取当前时间戳
    var timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000;
    console.log("当前时间戳为:" + timestamp);
    //获取当前时间
    var n = timestamp * 1000;
    var date = new Date(n);
    //年
    var Y = date.getFullYear();
    //月
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
    //日
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    //时
    var h = date.getHours();
    //分
    var m = date.getMinutes();
    //秒
    var s = date.getSeconds();
    var dataTame=Y + '-' + M + '-' + D + ' ' + h + ":" + m + ":" + s
    console.log("当前时间:", dataTame)
  },

输出:

微信小程序时间戳转时间,时间转时间戳,活动倒计时,清除定时器_第1张图片

2.日期转时间戳

  data_time() {
    var time = '2020-06-09 00:00:00';
    var repTime = time.replace(/-/g, '/'); //用正则主要是把“2019-05-20 00:00:00”转换成“2019/05/0 00:00:00”兼容ios
    console.log("返回时间:" + repTime);
    var timeTamp = Date.parse(repTime);
    console.log("返回时间戳:" + timeTamp)

  },

输出:

微信小程序时间戳转时间,时间转时间戳,活动倒计时,清除定时器_第2张图片

3.活动倒计时

 count_down: function () {
    // _____当前时间倒计时2自定义时间小时
    // var time = '2020-06-09 00:00:00';
    // var repTime = time.replace(/-/g, '/'); //用正则主要是把“2019-05-20 00:00:00”转换成“2019/05/0 00:00:00”兼容ios
    // console.log("返回时间:" + repTime);
    // var totalSecond = (Date.parse(repTime) + 24 * 60 * 60 * 1000 - Date.parse(new Date())) / 1000;
    // _____当前时间倒计时24小时
    var totalSecond = (Date.parse(new Date()) + 24 * 60 * 60 * 1000 - Date.parse(new Date())) / 1000;
    var interval = setInterval(function () {
      // 秒数
      var second = totalSecond;
      // 天数位
      var day = Math.floor(second / 3600 / 24);
      var dayStr = day.toString();
      if (dayStr.length == 1) dayStr = '0' + dayStr;

      // 小时位
      var hr = Math.floor((second - day * 3600 * 24) / 3600);
      var hrStr = hr.toString();
      if (hrStr.length == 1) hrStr = '0' + hrStr;

      // 分钟位
      var min = Math.floor((second - day * 3600 * 24 - hr * 3600) / 60);
      var minStr = min.toString();
      if (minStr.length == 1) minStr = '0' + minStr;

      // 秒位
      var sec = second - day * 3600 * 24 - hr * 3600 - min * 60;
      var secStr = sec.toString();
      if (secStr.length == 1) secStr = '0' + secStr;

      this.setData({
        countDownDay: dayStr,
        countDownHour: hrStr,
        countDownMinute: minStr,
        countDownSecond: secStr,
      });
      totalSecond--;
      if (totalSecond < 0) {
        clearInterval(interval);
        wx.showToast({
          title: '活动已结束',
        });
        this.setData({
          countDownDay: '00',
          countDownHour: '00',
          countDownMinute: '00',
          countDownSecond: '00',
        });
      }
    }.bind(this), 1000);
  },

输出:

 微信小程序时间戳转时间,时间转时间戳,活动倒计时,清除定时器_第3张图片

4.清除定时器

var aaaa;
//利用slice
function truncate(arr) {
  return arr.slice(0, -1);
}
Page({
  data: {
  },
  onShow() {
    console.log('yyyyyyyyyyyyyyyyyyy')
    clearInterval(aaaa)
    aaaa =setInterval(function () {
      that.getInfo(mchId)
    }, 5000)
  },
  onHide: function () {
    console.log('kkkkkkkkkkkkkkk')
    clearInterval(aaaa)
  },
})

 

你可能感兴趣的:(微信小程序,js,时间转时间戳,时间戳转时间,活动倒计时)