###部分代码介绍:
replace(/-/g, “/”):一般用于格式化日期,如
2016-1-1 格式化为 2016/1/1
/g 代表全局,所有的 " - "符号都替换为 " / "
getTime() 方法返回距 1970 年 1 月 1 日之间的毫秒数
js代码
//引入全局
const app = getApp();
var util = require('../../utils/date.js');
data{
sjc_time:'',//时间戳
time:'',//系统当前时间
end_time:'2019-12-31',//结束时间
start_time: '2019-12-11',//开始时间
}
onLoad: function(options) {
console.log('进入我的卡券列表')
// 获取劵的相关信息
var time = util.formatTime(new Date());
this.setData({
time:time
})
//判断身份与劵的状态
console.log('当前时间:',this.data.time);
var start_time = new Date(this.data.start_time.replace(/-/g, "/"));
this.setData({//赋值
start_time: this.data.start_time.replace(/-/g, "/")
end_time: this.data.end_time.replace(/-/g, "/")
})
var end_time = new Date(this.data.end_time.replace(/-/g, "/"));
console.log('num:', this.data.start_time);
console.log('num:', this.data.end_time);
var days = end_time.getTime() - start_time.getTime();
var day = parseInt(days / (1000 * 60 * 60 * 24));
if (day > 0) {
this.setData({
number: day
})
console.log('您的卡券还剩:', this.data.number + '天');
} else {
wx.showToast({
title: '日期出错了呀',
})
this.onShow();
}
},