dateSelect.js 5.82 KB
Component({

  properties: {
    show: {
      type: Boolean,
      observer: function (n) {
        if (n) {
          this.initDate()
        }
      }
    },
    selectPay:{
      type:Number
    }
  },


  data: {
    hours: [],
    days: [],
    minutes: [],
    dateValues: [],
    hourValues: [],
    minuteValues: [],
    loading: true
  },


  methods: {
    noScroll() { },
    hide(e) {
      let {
        name
      } = e.currentTarget.dataset
      let thisPage = wx.$router.getPageInstance()
      thisPage.setData({
        [name]: false
      })
      this.triggerEvent('close')
    },
    getDateList(starDay, endDay) {
      // const start = new Date(startDate);
      // const end = new Date(endDate);
      // const result = [];

      // let date = start;
      // while (date <= end) {
      //   const dateItem = date.toISOString().slice(0, 10)
      //   result.push(new Date(dateItem).format('yyyy.MM.dd'));
      //   date.setDate(date.getDate() + 1);
      // }
      const arr = [];
      const dates = [];

      // 设置两个日期UTC时间
      const db = new Date(starDay);
      const de = new Date(endDay);
      // 获取两个日期GTM时间
      const s = db.getTime() - 24 * 60 * 60 * 1000;
      const d = de.getTime() - 24 * 60 * 60 * 1000;

      // 获取到两个日期之间的每一天的毫秒数
      for (let i = s; i <= d;) {
        i = i + 24 * 60 * 60 * 1000;
        arr.push(parseInt(String(i)));
      }

      // 获取每一天的时间  YY-MM-DD
      for (const j in arr) {
        const time = new Date(arr[j]);
        const year = time.getFullYear();
        const mouth = time.getMonth() + 1 >= 10 ? time.getMonth() + 1 : '0' + (time.getMonth() + 1);
        const day = time.getDate() >= 10 ? time.getDate() : '0' + time.getDate();
        const YYMMDD = year + '/' + mouth + '/' + day;
        dates.push(YYMMDD);
      }
      this.getHourList(dates)
    },
    getHourList(days) {
      const hours = [], today = new Date();
      for (let hour = 0; hour < today.getHours() + 1; hour++) {
        hours.push(hour.toString().padStart(2, '0'));
      }
      this.getMinuteList(days, hours)
    },
    getMinuteList(days, hours) {
      const minutes = [];
      const today = new Date()
      for (let minute = 0; minute < today.getMinutes() + 1; minute++) {
        minutes.push(minute.toString().padStart(2, '0'));
      }
      // 设置默认时间
      let { dateValues, hourValues, minuteValues } = this.data
      const HOUR_STR = today.getHours().toString().padStart(2, '0')
      const MUNTUE_STR = today.getMinutes().toString().padStart(2, '0')
      // const defalutHIndex = hours.indexOf(HOUR_STR)
      // const defalutMIndex = minutes.indexOf(MUNTUE_STR)
      dateValues.push(days.length + 1)
      hourValues.push(hours.length + 1)
      minuteValues.push(minutes.length + 1)
      this.currentDate = new Date().format('yyyy/MM/dd')
      this.currentHour = HOUR_STR
      this.currentMinute = MUNTUE_STR
      this.setData({
        hours,
        days,
        minutes,
        dateValues,
        hourValues,
        minuteValues,
        loading: false
      }, () => {
        wx.nextTick(() => { });
        setTimeout(() => {
          this.setData({
            minuteValues,
            loading: false
          })
        }, 0);
      })
    },
    changeDate(e) {
      const { days } = this.data
      const { value } = e.detail
      const index = value[0]
      this.currentDate = days[index]
      // 选择过去时间需要重新初始化时分
      const today = new Date().format('yyyy/MM/dd')
      const timeType = this.currentDate != today ? 1 : 0
      this.getAllTimeOrPartTime(timeType)
    },
    changeHour(e) {
      const { hours, days, dateValues } = this.data
      const { value } = e.detail
      const index = value[0]
      this.currentHour = hours[index]

      // 处理当前天切换别的小时,别的分不能选后面分到60的问题
      // const dayIndex = dateValues[0]
      // this.currentDate = days[dayIndex]
      const today = new Date().format('yyyy/MM/dd')
      const timeType = this.currentDate != today ? 1 : 0
      this.getAllTimeOrPartTimeMinute(timeType)
    },
    changeMinutes(e) {
      const { minutes } = this.data
      const { value } = e.detail
      const index = value[0]
      this.currentMinute = minutes[index]
    },
    confirm() {
      let str = ''
      str = this.currentDate + ' ' + this.currentHour + ':' + this.currentMinute + ":00"
      const result = new Date(str).format('yyyy/MM/dd hh:mm:ss')
      this.triggerEvent('confirm', result)
    },
    initDate() {
      this.setData({
        dateValues: [],
        hourValues: [],
        minuteValues: [],
      })
      const today = new Date()
      this.getDateList('2019/01/01', new Date(today.getFullYear(), today.getMonth(), today.getDate()))
    },
    getAllTimeOrPartTime(type) {
      const today = new Date()
      const hours = [], minutes = [], hour_len = type == 1 ? 24 : today.getHours() + 1, minute_len = type == 1 ? 60 : today.getMinutes() + 1;
      for (let hour = 0; hour < hour_len; hour++) {
        hours.push(hour.toString().padStart(2, '0'));
      }
      for (let minute = 0; minute < minute_len; minute++) {
        minutes.push(minute.toString().padStart(2, '0'));
      }
      this.setData({
        hours,
        minutes,
      })
    },
    getAllTimeOrPartTimeMinute(type) {
      const today = new Date()
      const currentHour = today.getHours()
      const timeType = this.currentHour != currentHour ? 1 : 0
      const  minutes = [], minute_len = type == 1 || timeType==1 ? 60 : today.getMinutes() + 1;

      for (let minute = 0; minute < minute_len; minute++) {
        minutes.push(minute.toString().padStart(2, '0'));
      }
      this.setData({
        minutes,
      })
    }
  },
  //组件的生命周期
  lifetimes: {
    attached: function () {
      this.initDate()
    },
  },
})