小程序,JS 计算本月、五十天前、100天前的日期。

下面的代码,是我写小程序项目的片段,赋值给startTime是计算出来的日期,endTime默认当前日期,本文讲的是获取多少天前的日期,因此只需看那startTime就可以,希望能帮到大家!

//自由搜索

bindMoth: function(e) {

var index = e.currentTarget.dataset.index;

this.setData({

activeIndex: index

})

var name = this.data.quarterData[index].name;

var myDate = new Date();

if (name == '本月') {

this.setData({

saleOrderList: [],

startTime: util.formatTime()[0] + '-' + util.formatTime()[1] + '-' + "01",

endTime: util.formatTime()[0] + '-' + util.formatTime()[1] + '-' + util.formatTime()[2],

pageSize: 10,

pageIndex: 1

})

} else if (name == '50天') {

//获取50天前日期

var date = new Date(myDate - 1000 * 60 * 60 * 24 * 50);

var lastY = date.getFullYear();

var lastM = date.getMonth() + 1;

var lastD = date.getDate();

this.setData({

saleOrderList: [],

startTime: lastY + "-" + (lastM < 10 ? "0" + lastM : lastM) + "-" + (lastD < 10 ? "0" + lastD : lastD),

endTime: util.formatTime()[0] + '-' + util.formatTime()[1] + '-' + util.formatTime()[2],

pageSize: 10,

pageIndex: 1

})

} else if (name == '100天') {

//获取100天前日期

var date = new Date(myDate - 1000 * 60 * 60 * 24 * 100);

var lastY = date.getFullYear();

var lastM = date.getMonth() + 1;

var lastD = date.getDate();

this.setData({

saleOrderList: [],

startTime: lastY + "-" + (lastM < 10 ? "0" + lastM : lastM) + "-" + (lastD < 10 ? "0" + lastD : lastD),

endTime: util.formatTime()[0] + '-' + util.formatTime()[1] + '-' + util.formatTime()[2],

pageSize: 10,

pageIndex: 1

})

}else{

}

this.getdataList();

},

你可能感兴趣的:(小程序,js,微信小程序)