微信小程序日期时间选择器

使用原生组件picker,从底部弹起的滚动选择器

  • 日期选择器:mode = date
  • 时间选择器:mode = time
  • 多列选择器:mode = multiSelector

index.js

//index.js
//获取应用实例
const app = getApp()
const dateTimePicker = require('../../utils/dateTimePicker.js');

Page({
  data: {
    date: "2018-03-15",
    time: "12:01",
    dateTimeSecondArray: null,
    dateTimeSecond: null,
    dateTimeMinuteArray: null,
    dateTimeMinute: null,
    startYear: 2000,
    endYear: 2050
  },

  onLoad: function () {
    // 获取完整的年月日 时分秒,以及默认显示的数组
    var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    var obj2 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    // 精确到分的处理,将数组的秒去掉
    var lastArray = obj2.dateTimeArray.pop();
    var lastTime = obj2.dateTime.pop();
    console.log(obj1.dateTime)

    this.setData({
      dateTimeSecond: obj1.dateTime,
      dateTimeSecondArray: obj1.dateTimeArray,
      dateTimeMinute: obj2.dateTime,
      dateTimeMinuteArray: obj2.dateTimeArray
    });
  },

  changeDate(e) {
    console.log('changeDate: ' + e.detail.value);
    this.setData({ date: e.detail.value });
  },

  changeTime(e) {
    console.log('changeTime: ' + e.detail.value);
    this.setData({ time: e.detail.value });
  },

  changeDateTimeSecond(e) {
    console.log('changeDateTimeSecond: ' + e.detail.value);
    this.setData({ dateTime: e.detail.value });
  },

  changeDateTimeMinute(e) {
    console.log('changeDateTimeMinute: ' + e.detail.value);
    this.setData({ dateTime1: e.detail.value });
  },

  changeDateTimeSecondColumn(e) {
    var arr = this.data.dateTimeSecond, dateArr = this.data.dateTimeSecondArray;
    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
    this.setData({
      dateTimeSecondArray: dateArr,
      dateTimeSecond: arr
    });
  },

  changeDateTimeMinuteColumn(e) {
    var arr = this.data.dateTimeMinute, dateArr = this.data.dateTimeMinuteArray;
    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
    this.setData({
      dateTimeMinuteArray: dateArr,
      dateTimeMinute: arr
    });
  }

})

index.wxml

<!--index.wxml-->
<view class="container">
  <view class="weui-cells__title" style='padding-top: 30rpx; padding-bottom: 20rpx;'>小程序日期时间选择器 - AppBlog.CN</view>
  <view class="weui-cell weui-cell_input">
      <view class="weui-cell__hd">
        <view class="weui-label">日期</view>
      </view>
      <view class="weui-cell__bd">
        <picker mode="date" value="{{date}}" start="2015-09-01" end="2017-09-01" bindchange="bindDateChange">
          <view class="weui-input">{{date}}</view>
        </picker>
      </view>
  </view>
  <view class="weui-cell weui-cell_input">
      <view class="weui-cell__hd">
        <view class="weui-label">时间</view>
      </view>
      <view class="weui-cell__bd">
        <picker mode="time" value="{{time}}" start="09:01" end="21:01" bindchange="bindTimeChange">
          <view class="weui-input">{{time}}</view>
        </picker>
      </view>
  </view>
  <view class="weui-cell weui-cell_input">
      <view class="weui-cell__hd">
        <view class="weui-label">日期时间(秒)</view>
      </view>
      <view class="weui-cell__bd">
        <picker mode="multiSelector" value="{{dateTimeSecond}}" bindchange="changeDateTimeSecond" bindcolumnchange="changeDateTimeSecondColumn" range="{{dateTimeSecondArray}}">
          <view class="weui-input">
            {{dateTimeSecondArray[0][dateTimeSecond[0]]}}-{{dateTimeSecondArray[1][dateTimeSecond[1]]}}-{{dateTimeSecondArray[2][dateTimeSecond[2]]}} {{dateTimeSecondArray[3][dateTimeSecond[3]]}}:{{dateTimeSecondArray[4][dateTimeSecond[4]]}}:{{dateTimeSecondArray[5][dateTimeSecond[5]]}}
          </view>
        </picker>
      </view>
  </view>
  <view class="weui-cell weui-cell_input">
      <view class="weui-cell__hd">
        <view class="weui-label">日期时间(分)</view>
      </view>
      <view class="weui-cell__bd">
        <picker mode="multiSelector" value="{{dateTimeMinute}}" bindchange="changeDateTimeMinute" bindcolumnchange="changeDateTimeMinuteColumn" range="{{dateTimeMinuteArray}}">
          <view class="weui-input">
            {{dateTimeMinuteArray[0][dateTimeMinute[0]]}}-{{dateTimeMinuteArray[1][dateTimeMinute[1]]}}-{{dateTimeMinuteArray[2][dateTimeMinute[2]]}} {{dateTimeMinuteArray[3][dateTimeMinute[3]]}}:{{dateTimeMinuteArray[4][dateTimeMinute[4]]}}
          </view>
        </picker>
      </view>
  </view>
</view>

app.wxss

/**app.wxss**/
@import "weui.wxss";

.container {
  background-color: #F8F8F8;
  font-size: 16px;
  font-family: -apple-system-font,Helvetica Neue,Helvetica,sans-serif;
}

dateTimePicker.js

function withData(param) {
  return param < 10 ? '0' + param : '' + param;
}

function getLoopArray(start, end, dtype) {
  var start = start || 0;
  var end = end || 1;
  var array = [];
  for (var i = start; i <= end; i++) {
    if (dtype) {
      array.push(withData(i) + dtype);
    } else {
      array.push(withData(i));
    }
  }
  return array;
}

function getMonthDay(year,month){

  year = year.substr(0, year.length - 1);
  month = month.substr(0, month.length - 1);
  //console.log(year);
  //console.log(month);
  var flag = year % 400 == 0 || (year % 4 == 0 && year % 100 != 0), array = null;

  switch (month) {
    case '01':
    case '03':
    case '05':
    case '07':
    case '08':
    case '10':
    case '12':
      array = getLoopArray(1, 31,'日')
      break;
    case '04':
    case '06':
    case '09':
    case '11':
      array = getLoopArray(1, 30, '日')
      break;
    case '02':
      array = flag ? getLoopArray(1, 29, '日') : getLoopArray(1, 28, '日')
      break;
    default:
      array = '月份格式不正确,请重新输入!'
  }
  return array;
}

function getNewDateArry() {
  // 当前时间的处理
  var newDate = new Date();
  var year = withData(newDate.getFullYear()) + '年',
      mont = withData(newDate.getMonth() + 1) + '月',
      date = withData(newDate.getDate()) + '日',
      hour = withData(newDate.getHours()),
      minu = withData(newDate.getMinutes()),
      seco = withData(newDate.getSeconds());

  return [year, mont, date, hour, minu, seco];
}

function dateTimePicker(startYear, endYear, date) {
  // 返回默认显示的数组和联动数组的声明
  var dateTime = [], dateTimeArray = [[],[],[],[],[],[]];
  var start = startYear || 1978;
  var end = endYear || 2100;
  // 默认开始显示数据
  //console.log(date)
  var defaultDate = date ? [...date.split(' ')[0].split('-'), ...date.split(' ')[1].split(':')] : getNewDateArry();
  //console.log(defaultDate)
  //console.log(defaultDate[0])
  //console.log(defaultDate[1])
  // 处理联动列表数据
  /*年月日 时分秒*/ 
  dateTimeArray[0] = getLoopArray(start, end, '年');
  dateTimeArray[1] = getLoopArray(1, 12, '月');
  dateTimeArray[2] = getMonthDay(defaultDate[0], defaultDate[1]);
  dateTimeArray[3] = getLoopArray(0, 23);
  dateTimeArray[4] = getLoopArray(0, 59);
  dateTimeArray[5] = getLoopArray(0, 59);

  dateTimeArray.forEach((current, index) => {
    dateTime.push(current.indexOf(defaultDate[index]));
  });

  return {
    dateTimeArray: dateTimeArray,
    dateTime: dateTime
  }
}

module.exports = {
  dateTimePicker: dateTimePicker,
  getMonthDay: getMonthDay
}

微信小程序日期时间选择器

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/25/wechat-mini-program-date-and-time-selector/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
微信小程序日期时间选择器
使用原生组件picker,从底部弹起的滚动选择器 日期选择器:mode = date 时间选择器:mode = time 多列选择器:mode = multiSelector index.js //index.js /……
<<上一篇
下一篇>>
文章目录
关闭
目 录