小程序時間選擇器(精確到秒)


本文章只作為個人的一個筆記和收藏!!

 

 效果:

wxml:

<picker mode="multiSelector" value="{{dateTime}}" bindchange="changeDateTime" range="{{dateTimeArray}}">
  <view class="tui-picker-detail" id="eventTime">
    日期: {{showDate}}
  </view>
</picker>

 

js:

const app = getApp();
const fn = app.global;
import dateTimePicker from '../../../../utils/js/dateTimePicker.js';
Page({

  /**
   * formatDateTime 封裝格式化日期的方法
   * showDate 當前時間
   * dateTime 存儲當前選擇時間
   * dateTimeArray 存儲時間數組
   * endYear 默認為2100,這里我是當前時間往后推10年
   */
  data: {
    showDate: fn.formatDateTime('yyyy-MM-dd hh:mm:ss', new Date()),
    dateTime: null,
    dateTimeArray: null,
    endYear: new Date().getFullYear() + 20
  },
  onLoad: function (options) {
    // 獲取完整的年月日 時分秒,以及默認顯示的數組
    // 1、開始年份使用默認 1978年
    // 2、結束年份
    // 3、當前顯示時間
    var obj = dateTimePicker.dateTimePicker('', this.data.endYear, this.data.showDate);
    this.setData({
      dateTime: obj.dateTime,
      dateTimeArray: obj.dateTimeArray
    });
  },
  changeDateTime(e) {
    // arr此處使用新數據,否則數據不刷新
    var arr = e.detail.value, dateArr = this.data.dateTimeArray;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
    let showDate = `${dateArr[0][arr[0]]}-${dateArr[1][arr[1]]}-${dateArr[2][arr[2]]} ${dateArr[3][arr[3]]}:${dateArr[4][arr[4]]}:${dateArr[5][arr[5]]}`;
    this.setData({
      dateTimeArray: dateArr,
      dateTime: arr,
      showDate: showDate
    });
  }
})

 

導入的dateTimePicker.js

function withData(param) {
  return param < 10 ? '0' + param : '' + param;
}
function getLoopArray(start, end) {
  var start = start || 0;
  var end = end || 1;
  var array = [];
  for (var i = start; i <= end; i++) {
    array.push(withData(i));
  }
  return array;
}
function getMonthDay(year, 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;
  // 默認開始顯示數據
  var defaultDate = date ? [...date.split(' ')[0].split('-'), ...date.split(' ')[1].split(':')] : getNewDateArry();
  // 處理聯動列表數據
  /*年月日 時分秒*/
  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
}

 

格式化日期 formatDateTime 

fmt傳入格式 yyyy-MM-dd hh:mm:ss 可根據自己需求更改

/*
 * 格式化時間
 */
function formatDateTime(fmt, date) { 
  var o = {
    "M+": date.getMonth() + 1, //月份   
    "d+": date.getDate(), //
    "h+": date.getHours(), //小時   
    "m+": date.getMinutes(), //
    "s+": date.getSeconds(), //
    "q+": Math.floor((date.getMonth() + 3) / 3), //季度   
    "S": date.getMilliseconds() //毫秒   
  };
  if (/(y+)/.test(fmt))
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt))
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}

 

 

 

本文章到此結束,自己做個筆記,因為看了其他人的博客都很亂的感覺

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM