溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

微信小程序如何實(shí)現(xiàn)日期時(shí)分組件

發(fā)布時(shí)間:2021-10-28 10:53:38 來(lái)源:億速云 閱讀:175 作者:小新 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹了微信小程序如何實(shí)現(xiàn)日期時(shí)分組件,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

如圖所示效果

微信小程序如何實(shí)現(xiàn)日期時(shí)分組件

第一步新建picker組件文件

1,pickerTime.js組件代碼內(nèi)容

// component/pickerTime/pickerTime.js
Component({
  /**
   * 組件的屬性列表
   */
  properties: {
    date: {            // 屬性名
      type: null,     // 類型(必填),目前接受的類型包括:String, Number, Boolean, Object, Array, null(表示任意類型)
      value: null     // 屬性初始值(可選),如果未指定則會(huì)根據(jù)類型選擇一個(gè)
    },
    startDate: {
      type: null,     // 類型(必填),目前接受的類型包括:String, Number, Boolean, Object, Array, null(表示任意類型)
      value: null     // 屬性初始值(可選),如果未指定則會(huì)根據(jù)類型選擇一個(gè)
    },
    endDate: {
      type: null,     // 類型(必填),目前接受的類型包括:String, Number, Boolean, Object, Array, null(表示任意類型)
      value: null     // 屬性初始值(可選),如果未指定則會(huì)根據(jù)類型選擇一個(gè)
    },
    disabled: {
      type: null,     // 類型(必填),目前接受的類型包括:String, Number, Boolean, Object, Array, null(表示任意類型)
      value: false     // 屬性初始值(可選),如果未指定則會(huì)根據(jù)類型選擇一個(gè)
    },
    placeholder: {
      type: null,     // 類型(必填),目前接受的類型包括:String, Number, Boolean, Object, Array, null(表示任意類型)
      value: null     // 屬性初始值(可選),如果未指定則會(huì)根據(jù)類型選擇一個(gè)
    }
  },
 
  /**
   * 組件的初始數(shù)據(jù)
   */
  data: {
    pickerArray: [],//日期控件數(shù)據(jù)list
    pickerIndex: [],//日期控件選擇的index
    chooseIndex: [],//日期控件確認(rèn)選擇的index
    chooseArray: [],//日期控件確認(rèn)選擇后的list
    stDate: '',//開(kāi)始日期
    enDate: ''//結(jié)束日期
  },
 
  /**
   * 組件的方法列表
   */
  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());
      //默認(rèn)選擇3年內(nèi)
      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) {
        //如果存在開(kāi)始時(shí)間,則默認(rèn)設(shè)置結(jié)束時(shí)間為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) {
        //如果存在結(jié)束時(shí)間,不存在開(kāi)始時(shí)間 則默認(rèn)設(shè)置開(kāi)始時(shí)間為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: "默認(rèn)日期不在時(shí)間范圍內(nèi)"
        })
        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);
      //設(shè)置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);
    },
    /**
  * 
  * 獲取開(kāi)始日期 結(jié)束日期 中間日期
  * @param {date} newDate 默認(rèn)日期
  * @param {date} startDate 設(shè)置開(kāi)始日期
  * @param {date} stopDate 設(shè)置結(jié)束日期
  * @returns data 包含年月日時(shí)分?jǐn)?shù)組
  */
    _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;
      //判斷年份是否相同 相同則繼續(xù)
      if (nYear == tYear){
        //判斷結(jié)束年份 賦值月份 如果結(jié)束年份相同則把結(jié)束月份 一并賦值
        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 + "時(shí)" });
                    } else {
                      data.time.push({ id: i, name: i + "時(shí)" });
                    }
                  }
                  
                  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 + "時(shí)" });
                    } else {
                      data.time.push({ id: i, name: i + "時(shí)" });
                    }
                  }
                  
                  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 + "時(shí)" });
                    } else {
                      data.time.push({ id: i, name: i + "時(shí)" });
                    }
                  }
                  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 + "時(shí)" });
                    } else {
                      data.time.push({ id: i, name: i + "時(shí)" });
                    }
                  }
 
                  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 + "時(shí)" });
                  } else {
                    data.time.push({ id: i, name: i + "時(shí)" });
                  }
                }
                
                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 + "時(shí)" });
                  } else {
                    data.time.push({ id: i, name: i + "時(shí)" });
                  }
                }
                
                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 + "時(shí)" });
                  } else {
                    data.time.push({ id: i, name: i + "時(shí)" });
                  }
                }
                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 + "時(shí)" });
                  } else {
                    data.time.push({ id: i, name: i + "時(shí)" });
                  }
                }
                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 + "時(shí)" });
                } else {
                  data.time.push({ id: i, name: i + "時(shí)" });
                }
              }
 
              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 {//只需要開(kāi)始日期 因?yàn)榻Y(jié)束年份不同 所以不會(huì)用到結(jié)束日期
          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 + "時(shí)" });
                } else {
                  data.time.push({ id: i, name: i + "時(shí)" });
                }
              }
              
              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 + "時(shí)" });
                } else {
                  data.time.push({ id: i, name: i + "時(shí)" });
                }
              }
              
              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 + "時(shí)" });
              } else {
                data.time.push({ id: i, name: i + "時(shí)" });
              }
            }
            
            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 + "時(shí)" });
                } else {
                  data.time.push({ id: i, name: i + "時(shí)" });
                }
              }
              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 + "時(shí)" });
                } else {
                  data.time.push({ id: i, name: i + "時(shí)" });
                }
              }
            
              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 + "時(shí)" });
              } else {
                data.time.push({ id: i, name: i + "時(shí)" });
              }
            }
          
            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 + "時(shí)" });
            } else {
              data.time.push({ id: i, name: i + "時(shí)" });
            }
          }
          
          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
    },
    /**
  * 
  * 獲取本月天數(shù)
  * @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發(fā)送選擇改變,攜帶值為', 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
      };
      //首先獲取 修改后的日期 然后重新賦值列表數(shù)據(jù)
      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;
      //需要先判斷修改后的日期是否是正確的天數(shù) 不正確會(huì)導(dǎo)致日期錯(cuò)亂等未知情況
      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);
      //判斷修改后的日期是否在限制范圍內(nèi) 不在則重新賦值
      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
      })
    },
  },
  // 以下是舊式的定義方式,可以保持對(duì) <2.2.3 版本基礎(chǔ)庫(kù)的兼容
  attached() {
    // 在組件實(shí)例進(jìn)入頁(yè)面節(jié)點(diǎn)樹(shù)時(shí)執(zhí)行
    // 在組件實(shí)例進(jìn)入頁(yè)面節(jié)點(diǎn)樹(shù)時(shí)執(zhí)行
    // this._onInit();
  },
  ready() {
    // console.log('進(jìn)入ready外層節(jié)點(diǎn)=', this.data.date);
    this._onInit();
  },
  // 以下為新方法 >=2.2.3
  lifetimes: {
    attached() {
      // 在組件實(shí)例進(jìn)入頁(yè)面節(jié)點(diǎn)樹(shù)時(shí)執(zhí)行
      // this._onInit();
    },
    detached() {
      // 在組件實(shí)例被從頁(yè)面節(jié)點(diǎn)樹(shù)移除時(shí)執(zhí)行
    },
    ready() {
      // console.log('進(jìn)入ready節(jié)點(diǎn)=', this.data.date);
      this._onInit();
    }
  }
})

2.pickerTime.wxml內(nèi)容

<!--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>

在頁(yè)面中使用

1,demo.json中引入組件

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

2,頁(yè)面中使用 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: '請(qǐng)選擇時(shí)間'
 },
 onPickerChange: function (e) {
  this.setData({
    date: e.detail.dateString  //選中的數(shù)據(jù)
  })
 },
 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)
  },
  //監(jiān)聽(tīng)頁(yè)面加載
  onLoad: function (options) {
    let dayTime= this.getToday();
    let dayHour = "18:00";
    let endedTime1 = dayTime + " " + dayHour;
    this.setData({
      date: endedTime1
    })
  },

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“微信小程序如何實(shí)現(xiàn)日期時(shí)分組件”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI