微信小程序日期时分组件(年月日时分)

本文实例为大家分享了微信小程序日期时分组件的具体代码,供大家参考,具体内容如下

如图所示效果

第一步新建picker组件文件

1,pickerTime.js组件代码内容

// component/pickerTime/pickerTime.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    date: {            // 属性名
      type: null,     // 类型(必填),目前接受的类型包括:String, Number, Boolean, Object, Array, null(表示任意类型)
      value: null     // 属性初始值(可选),如果未指定则会根据类型选择一个
    },
    startDate: {
      type: null,     // 类型(必填),目前接受的类型包括:String, Number, Boolean, Object, Array, null(表示任意类型)
      value: null     // 属性初始值(可选),如果未指定则会根据类型选择一个
    },
    endDate: {
      type: null,     // 类型(必填),目前接受的类型包括:String, Number, Boolean, Object, Array, null(表示任意类型)
      value: null     // 属性初始值(可选),如果未指定则会根据类型选择一个
    },
    disabled: {
      type: null,     // 类型(必填),目前接受的类型包括:String, Number, Boolean, Object, Array, null(表示任意类型)
      value: false     // 属性初始值(可选),如果未指定则会根据类型选择一个
    },
    placeholder: {
      type: null,     // 类型(必填),目前接受的类型包括:String, Number, Boolean, Object, Array, null(表示任意类型)
      value: null     // 属性初始值(可选),如果未指定则会根据类型选择一个
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    pickerArray: [],//日期控件数据list
    pickerIndex: [],//日期控件选择的index
    chooseIndex: [],//日期控件确认选择的index
    chooseArray: [],//日期控件确认选择后的list
    stDate: '',//开始日期
    enDate: ''//结束日期
  },

  /**
   * 组件的方法列表
   */
  methods: {
    _onInit() {
      let date = new Date();
      if (this.data.date != null) {
        let str = this.data.date;
        str = str.replace(/-/g, "/");
        date = new Date(str);
      }
      let pickerArray = this.data.pickerArray;
      // console.log(date.getFullYear());
      //默认选择3年内
      let year = [];
      let month = [];
      let day = [];
      let time = [];
      let division = [];
      let startDate = '';
      let endDate = ''
      let tpData = {};
      if (this.data.startDate != null && this.data.endDate == null) {
        //如果存在开始时间,则默认设置结束时间为2099
        startDate = this._getDefaultDate(this.data.startDate);
        endDate = this._getDefaultDate("2099-12-31 23:59");
        tpData = this._getModify(date, startDate, endDate);
      }
      if (this.data.endDate != null && this.data.startDate == null) {
        //如果存在结束时间,不存在开始时间 则默认设置开始时间为1900
        startDate = this._getDefaultDate("1900-01-01 00:00");
        endDate = this._getDefaultDate(this.data.endDate);
        tpData = this._getModify(date, startDate, endDate);
      }
      if (this.data.endDate != null && this.data.startDate != null) {
        startDate = this._getDefaultDate(this.data.startDate);
        endDate = this._getDefaultDate(this.data.endDate);
        tpData = this._getModify(date, startDate, endDate);
      }
      // console.log(year);
      if (this.data.startDate == null && this.data.endDate == null) {
        startDate = this._getDefaultDate("1901-01-01 00:00");
        endDate = this._getDefaultDate("2099-12-31 23:59");
        tpData = this._getModify(date, startDate, endDate);
      }

      if (date > endDate || date < startDate) {
        this.setData({
          placeholder: "默认日期不在时间范围内"
        })
        return;
      }
      // console.log(division);
      pickerArray[0] = tpData.year;
      pickerArray[1] = tpData.month;
      pickerArray[2] = tpData.day;
      pickerArray[3] = tpData.time;
      pickerArray[4] = tpData.division;
      let mdate = {
        date: date,
        year: date.getFullYear() + '',
        month: date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1 + '',
        day: date.getDate() < 10 ? '0' + date.getDate() : date.getDate() + '',
        time: date.getHours() < 10 ? '0' + date.getHours() : date.getHours() + '',
        division: date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes() + ''
      }
      mdate.placeholder = mdate.year + '-' + mdate.month + '-' + mdate.day + ' ' + mdate.time + ':' + mdate.division;
      this.setData({
        pickerArray,
        pickerIndex: tpData.index,
        chooseIndex: tpData.index,
        chooseArray: pickerArray,
        placeholder: this.data.placeholder != null ? this.data.placeholder : mdate.placeholder,
        stDate: startDate,
        enDate: endDate
      })
      // console.log(date);
      //设置placeholder属性后 初始化不返回日期
      if (this.data.placeholder == null){
        this.triggerEvent('onPickerChange', mdate);
      }
      // console.log(this.data.pickerArray);
      // console.log(this._getNumOfDays(2018, 10));
    },
    /**
     *
     */
    _getDefaultDate(date) {
      date = date.replace(/-/g, "/");
      return new Date(date);
    },
    /**
  *
  * 获取开始日期 结束日期 中间日期
  * @param {date} newDate 默认日期
  * @param {date} startDate 设置开始日期
  * @param {date} stopDate 设置结束日期
  * @returns data 包含年月日时分数组
  */
    _getModify(newDate, startDate, stopDate) {
      let data = {
        year: [],
        month: [],
        day: [],
        time: [],
        division: [],
        index:[0,0,0,0,0]
      }
      let nYear = newDate.getFullYear();
      let nMonth = newDate.getMonth() + 1;
      let nDay = newDate.getDate();
      let nHours = newDate.getHours();
      let nMinutes = newDate.getMinutes();

      let tYear = startDate.getFullYear();
      let tMonth = startDate.getMonth() + 1;
      let tDay = startDate.getDate();
      let tHours = startDate.getHours();
      let tMinutes = startDate.getMinutes();

      let pYear = stopDate.getFullYear();
      let pMonth = stopDate.getMonth() + 1;
      let pDay = stopDate.getDate();
      let pHours = stopDate.getHours();
      let pMinutes = stopDate.getMinutes();
      for (let i = tYear; i <= pYear; i++) {
        data.year.push({ id: i, name: i + "年" });
      }
      data.index[0] = nYear - tYear;
      //判断年份是否相同 相同则继续
      if (nYear == tYear){
        //判断结束年份 赋值月份 如果结束年份相同则把结束月份 一并赋值
        if (nYear == pYear){
          for (let i = tMonth; i <= pMonth; i++) {
            data.month.push({ id: i, name: i + "月" });
          }
          data.index[1] = nMonth - tMonth < 0 ? 0 : nMonth - tMonth;

          if (nMonth == tMonth){
            if (nMonth == pMonth){
              for (let i = tDay; i <= pDay; i++) {
                data.day.push({ id: i, name: i + "日" });
              }
              data.index[2] = nDay - tDay < 0 ? 0 : nDay - tDay;

              if (nDay == tDay){
                if (nDay == pDay){
                  for (let i = tHours; i <= pHours; i++) {
                    if (i < 10) {
                      data.time.push({ id: i, name: "0" + i + "时" });
                    } else {
                      data.time.push({ id: i, name: i + "时" });
                    }
                  }

                  data.index[3] = nHours - tHours < 0 ? 0 : nHours - tHours;

                  if (nHours == tHours){
                    if (nHours == pHours){
                      for (let i = tMinutes; i <= pMinutes; i++) {
                        if (i < 10) {
                          data.division.push({ id: i, name: "0" + i + "分" });
                        } else {
                          data.division.push({ id: i, name: i + "分" });
                        }
                      }
                      data.index[4] = nMinutes - tMinutes < 0 ? 0 : nMinutes - tMinutes;
                    } else {
                      for (let i = tMinutes; i <= 59; i++) {
                        if (i < 10) {
                          data.division.push({ id: i, name: "0" + i + "分" });
                        } else {
                          data.division.push({ id: i, name: i + "分" });
                        }
                      }
                      data.index[4] = nMinutes - tMinutes < 0 ? 0 : nMinutes - tMinutes;
                    }
                  } else {
                    if (nHours == pHours){
                      for (let i = 0; i <= pMinutes; i++) {
                        if (i < 10) {
                          data.division.push({ id: i, name: "0" + i + "分" });
                        } else {
                          data.division.push({ id: i, name: i + "分" });
                        }
                      }
                      data.index[4] = nMinutes;
                    } else {
                      for (let i = 0; i <= 59; i++) {
                        if (i < 10) {
                          data.division.push({ id: i, name: "0" + i + "分" });
                        } else {
                          data.division.push({ id: i, name: i + "分" });
                        }
                      }
                      data.index[4] = nMinutes;
                    }
                  }
                } else {
                  for (let i = tHours; i <= 23; i++) {
                    if (i < 10) {
                      data.time.push({ id: i, name: "0" + i + "时" });
                    } else {
                      data.time.push({ id: i, name: i + "时" });
                    }
                  }

                  data.index[3] = nHours - tHours < 0 ? 0 : nHours - tHours;
                  if (nHours == tHours) {
                    for (let i = tMinutes; i <= 59; i++) {
                      if (i < 10) {
                        data.division.push({ id: i, name: "0" + i + "分" });
                      } else {
                        data.division.push({ id: i, name: i + "分" });
                      }
                    }
                    data.index[4] = nMinutes - tMinutes < 0 ? 0 : nMinutes - tMinutes;
                  } else {
                    for (let i = 0; i <= 59; i++) {
                      if (i < 10) {
                        data.division.push({ id: i, name: "0" + i + "分" });
                      } else {
                        data.division.push({ id: i, name: i + "分" });
                      }
                    }
                    data.index[4] = nMinutes;
                  }
                }
              } else {
                if (nDay == pDay){
                  for (let i = 0; i <= pHours; i++) {
                    if (i < 10) {
                      data.time.push({ id: i, name: "0" + i + "时" });
                    } else {
                      data.time.push({ id: i, name: i + "时" });
                    }
                  }
                  data.index[3] = nHours;
                  if (nHours == pHours){
                    for (let i = 0; i <= pMinutes; i++) {
                      if (i < 10) {
                        data.division.push({ id: i, name: "0" + i + "分" });
                      } else {
                        data.division.push({ id: i, name: i + "分" });
                      }
                    }
                    data.index[4] = nMinutes;
                  } else {
                    for (let i = 0; i <= 59; i++) {
                      if (i < 10) {
                        data.division.push({ id: i, name: "0" + i + "分" });
                      } else {
                        data.division.push({ id: i, name: i + "分" });
                      }
                    }
                    data.index[4] = nMinutes;
                  }
                } else {
                  for (let i = 0; i <= 23; i++) {
                    if (i < 10) {
                      data.time.push({ id: i, name: "0" + i + "时" });
                    } else {
                      data.time.push({ id: i, name: i + "时" });
                    }
                  }

                  data.index[3] = nHours;
                  // console.log(time);
                  for (let i = 0; i <= 59; i++) {
                    if (i < 10) {
                      data.division.push({ id: i, name: "0" + i + "分" });
                    } else {
                      data.division.push({ id: i, name: i + "分" });
                    }
                  }
                  data.index[4] = nMinutes;
                }
              }
            } else {
              let dayNum = this._getNumOfDays(nYear, nMonth);
              for (let i = tDay; i <= dayNum; i++) {
                data.day.push({ id: i, name: i + "日" });
              }
              data.index[2] = nDay - tDay < 0 ? 0 : nDay - tDay;
              if (nDay == tDay) {
                for (let i = tHours; i <= 23; i++) {
                  if (i < 10) {
                    data.time.push({ id: i, name: "0" + i + "时" });
                  } else {
                    data.time.push({ id: i, name: i + "时" });
                  }
                }

                data.index[3] = nHours - tHours < 0 ? 0 : nHours - tHours;
                if (nHours == tHours) {
                  for (let i = tMinutes; i <= 59; i++) {
                    if (i < 10) {
                      data.division.push({ id: i, name: "0" + i + "分" });
                    } else {
                      data.division.push({ id: i, name: i + "分" });
                    }
                  }
                  data.index[4] = nMinutes - tMinutes < 0 ? 0 : nMinutes - tMinutes;
                } else {
                  for (let i = 0; i <= 59; i++) {
                    if (i < 10) {
                      data.division.push({ id: i, name: "0" + i + "分" });
                    } else {
                      data.division.push({ id: i, name: i + "分" });
                    }
                  }
                  data.index[4] = nMinutes;
                }
              } else {
                for (let i = 0; i <= 23; i++) {
                  if (i < 10) {
                    data.time.push({ id: i, name: "0" + i + "时" });
                  } else {
                    data.time.push({ id: i, name: i + "时" });
                  }
                }

                data.index[3] = nHours;
                // console.log(time);
                for (let i = 0; i <= 59; i++) {
                  if (i < 10) {
                    data.division.push({ id: i, name: "0" + i + "分" });
                  } else {
                    data.division.push({ id: i, name: i + "分" });
                  }
                }
                data.index[4] = nMinutes;
              }
            }
          } else {
            if (nMonth == pMonth){
              for (let i = 1; i <= pDay; i++) {
                data.day.push({ id: i, name: i + "日" });
              }
              data.index[2] = nDay - 1;
              if (nDay == pDay){
                for (let i = 0; i <= pHours; i++) {
                  if (i < 10) {
                    data.time.push({ id: i, name: "0" + i + "时" });
                  } else {
                    data.time.push({ id: i, name: i + "时" });
                  }
                }
                data.index[3] = nHours;
                if (nHours == pHours){
                  for (let i = 0; i <= pMinutes; i++) {
                    if (i < 10) {
                      data.division.push({ id: i, name: "0" + i + "分" });
                    } else {
                      data.division.push({ id: i, name: i + "分" });
                    }
                  }
                  data.index[4] = nMinutes;
                } else {
                  for (let i = 0; i <= 59; i++) {
                    if (i < 10) {
                      data.division.push({ id: i, name: "0" + i + "分" });
                    } else {
                      data.division.push({ id: i, name: i + "分" });
                    }
                  }
                  data.index[4] = nMinutes;
                }
              } else {
                for (let i = 0; i <= 23; i++) {
                  if (i < 10) {
                    data.time.push({ id: i, name: "0" + i + "时" });
                  } else {
                    data.time.push({ id: i, name: i + "时" });
                  }
                }
                data.index[3] = nHours;
                // console.log(time);
                for (let i = 0; i <= 59; i++) {
                  if (i < 10) {
                    data.division.push({ id: i, name: "0" + i + "分" });
                  } else {
                    data.division.push({ id: i, name: i + "分" });
                  }
                }
                data.index[4] = nMinutes;
              }
            } else {
              let dayNum = this._getNumOfDays(nYear, nMonth);
              for (let i = 1; i <= dayNum; i++) {
                data.day.push({ id: i, name: i + "日" });
              }
              data.index[2] = nDay - 1;
              for (let i = 0; i <= 23; i++) {
                if (i < 10) {
                  data.time.push({ id: i, name: "0" + i + "时" });
                } else {
                  data.time.push({ id: i, name: i + "时" });
                }
              }

              data.index[3] = nHours;
              // console.log(time);
              for (let i = 0; i <= 59; i++) {
                if (i < 10) {
                  data.division.push({ id: i, name: "0" + i + "分" });
                } else {
                  data.division.push({ id: i, name: i + "分" });
                }
              }
              data.index[4] = nMinutes;
            }
          }
        } else {//只需要开始日期 因为结束年份不同 所以不会用到结束日期
          for (let i = tMonth; i <= 12; i++) {
            data.month.push({ id: i, name: i + "月" });
          }
          data.index[1] = nMonth - tMonth < 0 ? 0 : nMonth - tMonth;
          if (nMonth == tMonth){
            let dayNum = this._getNumOfDays(nYear, nMonth);
            for (let i = tDay; i <= dayNum; i++) {
             data.day.push({ id: i, name: i + "日" });
            }
            data.index[2] = nDay - tDay < 0 ? 0 : nDay - tDay;
            if (nDay == tDay){
              for (let i = tHours; i <= 23; i++) {
                if (i < 10) {
                  data.time.push({ id: i, name: "0" + i + "时" });
                } else {
                  data.time.push({ id: i, name: i + "时" });
                }
              }

              data.index[3] = nHours - tHours < 0 ? 0 : nHours - tHours;
              if (nHours == tHours){
                for (let i = tMinutes; i <= 59; i++) {
                  if (i < 10) {
                    data.division.push({ id: i, name: "0" + i + "分" });
                  } else {
                    data.division.push({ id: i, name: i + "分" });
                  }
                }
                data.index[4] = nMinutes - tMinutes < 0 ? 0 : nMinutes - tMinutes;
              } else {
                for (let i = 0; i <= 59; i++) {
                  if (i < 10) {
                    data.division.push({ id: i, name: "0" + i + "分" });
                  } else {
                    data.division.push({ id: i, name: i + "分" });
                  }
                }
                data.index[4] = nMinutes;
              }
            } else {
              for (let i = 0; i <= 23; i++) {
                if (i < 10) {
                  data.time.push({ id: i, name: "0" + i + "时" });
                } else {
                  data.time.push({ id: i, name: i + "时" });
                }
              }

              data.index[3] = nHours;
              // console.log(time);
              for (let i = 0; i <= 59; i++) {
                if (i < 10) {
                  data.division.push({ id: i, name: "0" + i + "分" });
                } else {
                  data.division.push({ id: i, name: i + "分" });
                }
              }
              data.index[4] = nMinutes;
            }
          } else {
            let dayNum = this._getNumOfDays(nYear, nMonth);
            for (let i = 1; i <= dayNum; i++) {
              data.day.push({ id: i, name: i + "日" });
            }
            data.index[2] = nDay - 1;
            for (let i = 0; i <= 23; i++) {
              if (i < 10) {
                data.time.push({ id: i, name: "0" + i + "时" });
              } else {
                data.time.push({ id: i, name: i + "时" });
              }
            }

            data.index[3] = nHours;
            // console.log(time);
            for (let i = 0; i <= 59; i++) {
              if (i < 10) {
                data.division.push({ id: i, name: "0" + i + "分" });
              } else {
                data.division.push({ id: i, name: i + "分" });
              }
            }
            data.index[4] = nMinutes;
          }
        }
      } else {
        if (nYear == pYear) {
          for (let i = 1; i <= pMonth; i++) {
            data.month.push({ id: i, name: i + "月" });
          }
          data.index[1] = nMonth - 1;
          if (nMonth == pMonth){
            for (let i = 1; i <= pDay; i++) {
              data.day.push({ id: i, name: i + "日" });
            }
            data.index[2] = nDay - 1;
            if (nDay == pDay){
              for (let i = 0; i <= pHours; i++) {
                if (i < 10) {
                  data.time.push({ id: i, name: "0" + i + "时" });
                } else {
                  data.time.push({ id: i, name: i + "时" });
                }
              }
              data.index[3] = nHours;
              if (nHours == pHours){
                for (let i = 0; i <= pMinutes; i++) {
                  if (i < 10) {
                    data.division.push({ id: i, name: "0" + i + "分" });
                  } else {
                    data.division.push({ id: i, name: i + "分" });
                  }
                }
                data.index[4] = nMinutes;
              } else {
                for (let i = 0; i <= 59; i++) {
                  if (i < 10) {
                    data.division.push({ id: i, name: "0" + i + "分" });
                  } else {
                    data.division.push({ id: i, name: i + "分" });
                  }
                }
                data.index[4] = nMinutes;
              }
            } else {
              for (let i = 0; i <= 23; i++) {
                if (i < 10) {
                  data.time.push({ id: i, name: "0" + i + "时" });
                } else {
                  data.time.push({ id: i, name: i + "时" });
                }
              }

              data.index[3] = nHours;
              // console.log(time);
              for (let i = 0; i <= 59; i++) {
                if (i < 10) {
                  data.division.push({ id: i, name: "0" + i + "分" });
                } else {
                  data.division.push({ id: i, name: i + "分" });
                }
              }
              data.index[4] = nMinutes;
            }
          } else {
            let dayNum = this._getNumOfDays(nYear, nMonth);
            for (let i = 1; i <= dayNum; i++) {
              data.day.push({ id: i, name: i + "日" });
            }
            data.index[2] = nDay - 1;
            for (let i = 0; i <= 23; i++) {
              if (i < 10) {
                data.time.push({ id: i, name: "0" + i + "时" });
              } else {
                data.time.push({ id: i, name: i + "时" });
              }
            }

            data.index[3] = nHours;
            // console.log(time);
            for (let i = 0; i <= 59; i++) {
              if (i < 10) {
                data.division.push({ id: i, name: "0" + i + "分" });
              } else {
                data.division.push({ id: i, name: i + "分" });
              }
            }
            data.index[4] = nMinutes;
          }
        } else {
          for (let i = 1; i <= 12; i++) {
            data.month.push({ id: i, name: i + "月" });
          }
          data.index[1] = nMonth - 1;
          let dayNum = this._getNumOfDays(nYear, nMonth);
          for (let i = 1; i <= dayNum; i++) {
            data.day.push({ id: i, name: i + "日" });
          }
          data.index[2] = nDay - 1;
          for (let i = 0; i <= 23; i++) {
            if (i < 10) {
              data.time.push({ id: i, name: "0" + i + "时" });
            } else {
              data.time.push({ id: i, name: i + "时" });
            }
          }

          data.index[3] = nHours;
          // console.log(time);
          for (let i = 0; i <= 59; i++) {
            if (i < 10) {
              data.division.push({ id: i, name: "0" + i + "分" });
            } else {
              data.division.push({ id: i, name: i + "分" });
            }
          }
          data.index[4] = nMinutes;
        }
      }
      return data
    },
    /**
  *
  * 获取本月天数
  * @param {number} year
  * @param {number} month
  * @param {number} [day=0] 0为本月0最后一天的
  * @returns number 1-31
  */
    _getNumOfDays(year, month, day = 0) {
      return new Date(year, month, day).getDate()
    },
    pickerChange: function (e) {
      // console.log('picker发送选择改变,携带值为', e.detail.value)
      let indexArr = e.detail.value;
      const year = this.data.pickerArray[0][indexArr[0]].id;
      const month = this.data.pickerArray[1][indexArr[1]].id;
      const day = this.data.pickerArray[2][indexArr[2]].id;
      const time = this.data.pickerArray[3][indexArr[3]].id;
      const division = this.data.pickerArray[4][indexArr[4]].id;
      let date = {
        date: new Date(year + '-' + month + '-' + day + ' ' + time + ':' + division),
        year: year + '',
        month: month < 10 ? '0' + month : month + '',
        day: day < 10 ? '0' + day : day + '',
        time: time < 10 ? '0' + time : time + '',
        division: division < 10 ? '0' + division : division + ''
      }
      date.dateString = date.year + '-' + date.month + '-' + date.day + ' ' + date.time + ':' + date.division;
      // console.log(date);
      this.setData({
        chooseIndex: e.detail.value,
        chooseArray: this.data.pickerArray,
        placeholder: date.dateString
      })
      this.triggerEvent('onPickerChange', date);
    },
    pickerColumnChange: function (e) {
      // console.log('修改的列为', e.detail.column, ',值为', e.detail.value);
      let data = {
        pickerArray: this.data.pickerArray,
        pickerIndex: this.data.pickerIndex
      };
      //首先获取 修改后的日期 然后重新赋值列表数据
      data.pickerIndex[e.detail.column] = e.detail.value;
      let cYear = data.pickerArray[0][data.pickerIndex[0]].id;
      let cMonth = data.pickerArray[1][data.pickerIndex[1]].id;
      let cDay = data.pickerArray[2][data.pickerIndex[2]].id;
      let cTime = data.pickerArray[3][data.pickerIndex[3]].id;
      let cDivision = data.pickerArray[4][data.pickerIndex[4]].id;
      //需要先判断修改后的日期是否是正确的天数 不正确会导致日期错乱等未知情况
      let daysn = this._getNumOfDays(cYear, cMonth);
      //不正确 重新赋值
      if (cDay > daysn){
        cDay = daysn;
      }

      // console.log(cYear + '-' + cMonth + '-' + cDay + ' ' + cTime + ':' + cDivision);
      let newDate = this._getDefaultDate(cYear + '-' + cMonth + '-' + cDay + ' ' + cTime + ':' + cDivision);
      //判断修改后的日期是否在限制范围内 不在则重新赋值
      if (newDate > this.data.enDate) {
        newDate = this.data.enDate;
      }
      if (newDate < this.data.stDate){
        newDate = this.data.stDate;
      }
      let tpData = this._getModify(newDate, this.data.stDate, this.data.enDate);
      data.pickerArray[0] = tpData.year;
      data.pickerArray[1] = tpData.month;
      data.pickerArray[2] = tpData.day;
      data.pickerArray[3] = tpData.time;
      data.pickerArray[4] = tpData.division;
      data.pickerIndex = tpData.index;

      for (let i = 0; i <=4; i++) {
        if (data.pickerArray[i].length - 1 < data.pickerIndex[i]) {
          data.pickerIndex[i] = data.pickerArray[i].length - 1;
        }
      }
      this.setData(data);
    },
    pickerCancel: function (e) {
      // console.log("取消");
      this.setData({
        pickerIndex: this.data.chooseIndex,
        pickerArray: this.data.chooseArray
      })
    },
  },
  // 以下是旧式的定义方式,可以保持对 <2.2.3 版本基础库的兼容
  attached() {
    // 在组件实例进入页面节点树时执行
    // 在组件实例进入页面节点树时执行
    // this._onInit();
  },
  ready() {
    // console.log('进入ready外层节点=', this.data.date);
    this._onInit();
  },
  // 以下为新方法 >=2.2.3
  lifetimes: {
    attached() {
      // 在组件实例进入页面节点树时执行
      // this._onInit();
    },
    detached() {
      // 在组件实例被从页面节点树移除时执行
    },
    ready() {
      // console.log('进入ready节点=', this.data.date);
      this._onInit();
    }
  }
})

2.pickerTime.wxml内容

<!--component/pickerTime/pickerTime.wxml-->
<view>
  <picker disabled="{{disabled}}" mode="multiSelector" bindchange="pickerChange" bindcolumnchange="pickerColumnChange" bindcancel ="pickerCancel" value="{{pickerIndex}}" range="{{pickerArray}}" range-key="{{'name'}}">
    <view>
      {{placeholder}}
    </view>
  </picker>
</view>

在页面中使用

1,demo.json中引入组件

{
  "navigationBarTitleText": "demo",
  "usingComponents": {
    "pickerTime": "/components/pickerTime/pickerTime"
  }
}

2,页面中使用 wxml

<pickerTime placeholder="{{placeholder}}" date="{{date}}" bind:onPickerChange="onPickerChange"
startDate="{{startDate}}" endDate="{{endDate}}">
</pickerTime>

3.demo.js

data:{
 date: '2019-01-01 13:37',
 startDate: '2019-01-01 12:37',
 endDate: '2029-03-12 12:38',
 placeholder: '请选择时间'
 },
 onPickerChange: function (e) {
  this.setData({
    date: e.detail.dateString  //选中的数据
  })
 },
 toDouble: function (num) {
  if (num >= 10) {//大于10
    return num;
  } else {//0-9
    return '0' + num
  }
  },
 getToday: function () {
  let date = new Date();
  let year = date.getFullYear();
  let month = date.getMonth() + 1;
  let day = date.getDate();
  return year + '-' + this.toDouble(month) + '-' + this.toDouble(day)
  },
  //监听页面加载
  onLoad: function (options) {
    let dayTime= this.getToday();
    let dayHour = "18:00";
    let endedTime1 = dayTime + " " + dayHour;
    this.setData({
      date: endedTime1
    })
  },

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • 微信小程序实现日期格式化和倒计时

    本文实例为大家分享了微信小程序实现日期格式化和倒计时的具体代码,供大家参考,具体内容如下 首先看看日期怎么格式化 第一种: Date.prototype.Format = function (fmt) { //author: meizz var o = { "M+": this.getMonth() + 1, //月份 "d+": this.getDate(), //日 "h+": this.getHours(), //小时 "m+&q

  • 微信小程序时间戳转日期的详解

    正文: // util.js //时间戳转换成日期时间 function js_date_time(unixtime) { var dateTime = new Date(parseInt(unixtime) * 1000) var year = dateTime.getFullYear(); var month = dateTime.getMonth() + 1; var day = dateTime.getDate(); var hour = dateTime.getHours(); var

  • 微信小程序日期时间选择器使用方法

    本文实例为大家分享了精确到秒的微信小程序日期时间选择器,供大家参考,具体内容如下 效果图 实现原理 利用微信小程序的picker组件的多列选择器实现! WXML <view class="tui-picker-content"> <view class="tui-picker-name">时间选择器(选择时分)</view> <picker mode="time" value="{{time}}

  • 微信小程序 滚动选择器(时间日期)详解及实例代码

    微信小程序  滚动选择器(时间日期)详解 微信小程序自己封装了很多控件,用起来确实很方便,如果这是Android里面,还需要自己去定义,不废话,效果图: 一起来看看怎么实现的呢?看完你应该就该说,尼玛,这就行啦-. 这个效果呢,要用到picker组件,动画从底部弹起的滚动选择器,现支持三种选择器,通过mode来区分,分别是普通选择器,时间选择器,日期选择器,默认是普通选择器. 看下相应的属性: 具体的来看看代码,布局: <view class="section" > <

  • 详解微信小程序获取当前时间及日期的方法

    获取当前时间 首先,在要获取时间的.js文件中加载util.js文件 然后在onload方法中,调用util.js中的formatTime方法获取当前时间 //获取当前时间 // 调用函数时,传入new Date()参数,返回值是日期和时间 var TIME = util.formatTime(new Date()); this.setData({ time: TIME, }); 这样就获取到了当前时间,但是我们发现在util.js中并没有获取当前日期的方法. 没事,别慌! 这个时候我们去看下u

  • 微信小程序中时间戳和日期的相互转换问题

    在微信开发小程序时,后台传入的诗句可能是 时间戳 而不是日期或者需要把日期转换成时间戳来做出相应的处理时我们将用到时间戳和日期的相互转换微信小程序里,时间戳转化为日期格式,支持自定义.拷贝至项目utils/utils.js中,并注意在js中声明下: module.exports = { formatTime: formatTime, // 日期转时间戳 formatTimeTwo: formatTimeTwo // 时间戳转日期 } utils/utils.js 里面写函数 function f

  • 微信小程序之picker日期和时间选择器

    下面来介绍小picker,分三种样式: 默认的自己可以定义数据的 mode="time"是时间选择器 mode="date"是日期选择器 跟其他的一样先来看下picker.wxml <view class="page"> <view class="page__hd"> <text class="page__title">picker</text> <te

  • 微信小程序的日期选择器的实例详解

    微信小程序的日期选择器的实例详解 前言: 关于微信小程序中的日期选择器大家用过都会发现有个很大的问题,就是在2月的时候会有31天,没有进行对闰年的判断等各种情况.看了官方文档提供的源码后进行了一些修改,测试修复了上面所说的bug! 下面源码: <!---js---> const date = new Date();//获取系统日期 const years = [] const months = [] const days = [] const bigMonth = [1,3,5,7,8,10,

  • 微信小程序日历/日期选择插件使用方法详解

    微信小程序日历选择器插件点击日历日期可以获取到年月日,具体内容如下 wxml <view class="canlendarBgView"> <view class="canlendarView"> <view class="canlendarTopView"> <view class="leftBgView" bindtap="handleCalendar" dat

  • 微信小程序 选择器(时间,日期,地区)实例详解

    微信小程序 选择器(时间,日期,地区) 微信小程序 开发由于本人最近学习微信小程序的开发,根据自己的实践结果整理了下结果,对日期选择器,时间选择器,地区选择器做的实例,有不对的地方,希望大家指正. 用微信封装好的控件感觉很好,为我们开发人员省去了很多麻烦.弊端就是不能做大量的自定义.今天试用了选择器. 上gif: 上代码: 1.index.js //index.js //获取应用实例 var app = getApp() Page({ data: { date: '2016-11-08', ti

随机推荐