公司需要用到小程序日历段时间选择模块,找了好多都不太靠谱,最终参考别人思考自己需求,开发了这个源码,提供大家使用!
主要代码:
dateData: function () {
let dataAll = []//总日历数据
let dataAll2 = []//总日历数据
let dataMonth = []//月日历数据
let date = new Date//当前日期
let year = date.getFullYear()//当前年
let week = date.getDay();//当天星期几
let weeks = []
let month = date.getMonth() + 1//当前月份
let day = date.getDate()//当天
let daysCount = 365*2;//一共显示多少天
let dayscNow = 0//计数器
let nowMonthList = []
let monthList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]//月份列表
for (let i = 1; i < 13; i++) {
nowMonthList.push(i)
}
let yearmonthday = year + '' + ((month < 10) ? ('0' + month) : month) + '' + ((day < 10) ? ('0' + day) : day);
let yearmonth = year + '' + ((month < 10) ? ('0' + month) : month);
let yearList = []//年份最大可能
for (let i = 0; i < 2; i++) {
yearList.push(year - 1 + i)
}
let leapYear = function (Year) {//判断是否闰年
if (((Year % 4) == 0) && ((Year % 100) != 0) || ((Year % 400) == 0)) {
return (true);
} else { return (false); }
}
for (let i = 0; i < yearList.length; i++) {//遍历年
let mList
mList = monthList
for (let j = 0; j < mList.length; j++) {//循环月份
dataMonth = []
let t_days = [31, 28 + leapYear(yearList[i]), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
for (let k = 0; k < t_days[j]; k++) {//循环每天
dayscNow++
let nowData
if (dayscNow < daysCount) {//如果计数器没满
let days = k + 1
if (days < 10) {
days = "0" + days
}
let month = mList[j];
if (month < 10) {
month = "0" + month
}
var selecteds = 0;
var dates = yearList[i] + "" + month + "" + days;
var yearmonths = yearList[i] + "" + month;
//如果两个都有值
if(this.data.startdate && this.data.enddate){
if (dates>=this.data.startdates && dates<=this.data.enddates){
selecteds = 1;
}
} else if (this.data.startdate && !this.data.enddate){
if (dates == this.data.startdates) {
selecteds = 1;
}
}
var todays = 0;
var tomonth = 0;
if (yearmonthday == dates){
todays = 1;
}
if (yearmonth == yearmonths){
tomonth = 1;
}
nowData = {//组装自己需要的数据
year: yearList[i],
month: month,
day: days,
todays: todays,
tomonth: tomonth,
date: yearList[i] + "" + month + "" + days,
selected: selecteds,
re: yearList[i] + "-" + month + "-" + days,
}
dataMonth.push(nowData)
if (k == 0) {
let date = new Date(yearList[i] + "-" + month + "-" + days)
let weekss = date.getDay()//获取每个月第一天是周几
weeks.push(weekss)
}
} else {
break
}
}
dataAll.push(dataMonth)
}
}
for (let i = 0; i < dataAll.length; i++) {
if (dataAll[i].length != 0) {
dataAll2.push(dataAll[i]);
}
}
this.setData({
date: dataAll2,
weeks: weeks
})
}, selectday:function(e){
if (this.data.enddate){
this.setData({
startdate: e.currentTarget.dataset.dates,
startdates: e.currentTarget.dataset.datestr,
enddate:'',
enddates: '',
showBottom: false
});
}else{
if (this.data.startdate && (this.data.startdates <= e.currentTarget.dataset.datestr)){
this.setData({
enddate: e.currentTarget.dataset.dates,
enddates: e.currentTarget.dataset.datestr,
showBottom:true
});
}else{
this.setData({
startdate: e.currentTarget.dataset.dates,
startdates: e.currentTarget.dataset.datestr,
enddate: '',
enddates: '',
showBottom: false
});
}
}
this.dateData();
}, backTap:function(e){
wx.navigateBack({
delta: 1
})
}, okTap:function(e){
var pages = getCurrentPages();
var currPage = pages[pages.length - 1]; //当前页面
var prevPage = pages[pages.length - 2]; //上一个页面
prevPage.setData({
startdate:this.data.startdate,
enddate:this.data.enddate
});
wx.navigateBack({
delta: 1
})
}
下载地址:https://download.csdn.net/download/huangsiqing88/11128276