123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- // components/date-time-picker/date-time-picker.js
- const leftPad0 = function(v, n) {
- if (!v) {
- v = "";
- }
- let prefix = "";
- for (let i = 0; i < n; i++) {
- prefix += "0";
- }
- return (prefix + v).substr(-n);
- };
- const stringToDate = function(str) {
- str = str.replace(/-/g, "/");
- return new Date(str);
- };
- const isLeapYear = function(year) {
- if (((year % 4) == 0) && ((year % 100) != 0) || ((year % 400) == 0)) {
- return true;
- }
- return false;
- };
- const now = new Date();
- const years = [];
- const beginYear = 2020;
- for (var i = beginYear; i <= (now.getFullYear() + 10); i++) {
- years.push(i + "年");
- }
- const months = [];
- for (var i = 0; i < 12; i++) {
- months.push(leftPad0(i + 1, 2) + "月");
- }
- const days = [];
- for (var i = 0; i < 31; i++) {
- days.push(leftPad0(i + 1, 2) + "日");
- }
- const hours = [];
- for (var i = 0; i < 24; i++) {
- hours.push(leftPad0(i, 2) + "时");
- }
- const minutes = [];
- for (var i = 0; i < 60; i++) {
- minutes.push(leftPad0(i, 2) + "分");
- }
- // const seconds = [];
- // for (var i = 0; i < 60; i++) {
- // seconds.push(leftPad0(i, 2) + "秒");
- // }
- Component({
- /**
- * 组件的属性列表
- */
- properties: {
- value: String,
- dateValue: {
- type: Date
- }
- },
- /**
- * 组件的初始数据
- */
- data: {
- // valueArray: [0, 0, 0, 0, 0, 0],
- valueArray: [0, 0, 0, 0, 0],
- rangeValues: [
- years,
- months,
- days,
- hours,
- minutes,
- // seconds
- ],
- pickerYear: beginYear,
- pickerMonth: 1
- },
- observers: {
- value: function(v) {
- this.setData({
- valueArray: this._dateToValueArray(stringToDate(v))
- })
- },
- dateValue: function(date) {
- this.setData({
- valueArray: this._dateToValueArray(date)
- })
- },
- valueArray: function(v) {
- this._settMonthDays(v[0] + beginYear, v[1] + 1);
- }
- },
- /**
- * 组件的方法列表
- */
- methods: {
- _dateToValueArray(date) {
- // return [date.getFullYear() - beginYear, date.getMonth(), date.getDate() - 1, date.getHours(), date.getMinutes(), date.getSeconds()];
- return [date.getFullYear() - beginYear, date.getMonth(), date.getDate() - 1, date.getHours(), date.getMinutes()];
- },
- _settMonthDays(year, month) {
- let monthDays = 31;
- switch (month) {
- case 2:
- monthDays = 28;
- if (isLeapYear(year)) {
- monthDays = 29;
- }
- break;
- case 4:
- case 6:
- case 9:
- case 11:
- monthDays = 30;
- break;
- }
- let days = [];
- for (let i = 0; i < monthDays; i++) {
- days.push(leftPad0(i + 1, 2) + "日");
- }
- this.setData({
- pickerYear: year,
- pickerMonth: month,
- "rangeValues[2]": days
- });
- },
- handleCancel(e) {
- this.setData({
- valueArray: this.data.valueArray
- })
- },
- handleColumnChange(e) {
- if (e.detail.column > 1) return false;
- let year = this.data.pickerYear;
- let month = this.data.pickerMonth;
- if (e.detail.column == 0) {
- year = e.detail.value + beginYear;
- } else if (e.detail.column == 1) {
- month = e.detail.value + 1;
- }
- this._settMonthDays(year, month);
- },
- handleValueChange(e) {
- let dateArr = [];
- for (let i in e.detail.value) {
- let v = this.data.rangeValues[i][e.detail.value[i]];
- dateArr.push(v.toString().substr(0, v.length - 1))
- }
- // let dateString = dateArr[0] + "-" + dateArr[1] + "-" + dateArr[2] + " " + dateArr[3] + ":" + dateArr[4] + ":" + dateArr[5];
- let dateString = dateArr[0] + "-" + dateArr[1] + "-" + dateArr[2] + " " + dateArr[3] + ":" + dateArr[4];
- this.triggerEvent('change', {
- date: stringToDate(dateString),
- dateString
- })
- }
- }
- })
|