小程序判断当前时间是否在所给时间之内

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hhy2014yatan/article/details/82021152

描述:活动给出开始时间与结束时间,判断当前时间是否在活动时间之内

(1)由于给出的时间格式为:年月日,与util中的当前时间格式不同,因此需要重新写一个关于日期的函数

const formatDate = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  return [year, month, day].map(formatNumber).join('/')
}
module.exports = {
  
  formatDate: formatDate
  
}

.join('/')处的分隔符可以换成其他的符号,跟所给时间的分隔符一致就好

(2)在对应文件中引用util.js文件

var util = require('../../../utils/util.js'); 

(3)在data中添加:

time: util.formatDate(new Date()),

(4)编写函数:

onLoad: function () {
    // 调用函数时,传入new Date()参数,返回值是日期和时间  
    var time = util.formatDate(new Date());
    // 再通过setData更改Page()里面的data,动态更新页面的数据  
    this.setData({
      time: time
    });
  }    

(5)在wxml页面进行判断

wx:if="{{time >= item.startdate && time <= item.enddate}}"

猜你喜欢

转载自blog.csdn.net/hhy2014yatan/article/details/82021152
今日推荐