小程序日历选择插件源码

公司需要用到小程序日历段时间选择模块,找了好多都不太靠谱,最终参考别人思考自己需求,开发了这个源码,提供大家使用!

主要代码:

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值