Commit 450b9d2d by 李伟康

refactor(DateTime): 优化日期时间选择器组件代码结构

- 统一函数声明语法,移除不必要的空格
- 将日期处理逻辑从原生Date对象迁移到dayjs库
- 添加日期有效性验证以避免无效日期导致的错误
- 重构月份和日期格式化逻辑,提高代码可读性
- 添加CSS样式中的网格布局和指示器样式增强
- 优化时间选择逻辑,确保今日时间限制功能正常工作
1 parent 95b3abe0
...@@ -29,7 +29,7 @@ import dayjs from 'dayjs' ...@@ -29,7 +29,7 @@ import dayjs from 'dayjs'
export default { export default {
name: 'DateTime', name: 'DateTime',
inject: ['userInfo'], inject: ['userInfo'],
data () { data() {
return { return {
hours: [], hours: [],
days: [], days: [],
...@@ -44,18 +44,18 @@ export default { ...@@ -44,18 +44,18 @@ export default {
}, },
mounted () { mounted() {
this.initDate() this.initDate()
}, },
methods: { methods: {
initDate () { initDate() {
this.dateValues = [] this.dateValues = []
this.hourValues = [] this.hourValues = []
this.minuteValues = [] this.minuteValues = []
const today = new Date() const today = new Date()
this.getDateList('2019/01/01', new Date(today.getFullYear(), today.getMonth(), today.getDate())) this.getDateList('2019/01/01', new Date(today.getFullYear(), today.getMonth(), today.getDate()))
}, },
getDateList (starDay, endDay) { getDateList(starDay, endDay) {
const arr = []; const arr = [];
const dates = []; const dates = [];
...@@ -74,16 +74,19 @@ export default { ...@@ -74,16 +74,19 @@ export default {
// 获取每一天的时间 YY-MM-DD // 获取每一天的时间 YY-MM-DD
for (const j in arr) { for (const j in arr) {
const time = new Date(arr[j]); const time = dayjs(arr[j]);
const year = time.getFullYear(); if (!time.isValid()) continue; // 跳过无效日期
const mouth = time.getMonth() + 1 >= 10 ? time.getMonth() + 1 : '0' + (time.getMonth() + 1); const year = time.year();
const day = time.getDate() >= 10 ? time.getDate() : '0' + time.getDate(); const month = time.month() + 1; // 注意:dayjs 的 month() 返回 0-11
const YYMMDD = year + '/' + mouth + '/' + day; const day = time.date();
const mouthStr = month >= 10 ? month : '0' + month;
const dayStr = day >= 10 ? day : '0' + day;
const YYMMDD = year + '/' + mouthStr + '/' + dayStr;
dates.push(YYMMDD); dates.push(YYMMDD);
} }
this.getHourList(dates) this.getHourList(dates)
}, },
getHourList (days) { getHourList(days) {
const hours = [], today = new Date(); const hours = [], today = new Date();
for (let hour = 0; hour < today.getHours() + 1; hour++) { for (let hour = 0; hour < today.getHours() + 1; hour++) {
hours.push(hour.toString().padStart(2, '0')); hours.push(hour.toString().padStart(2, '0'));
...@@ -91,7 +94,7 @@ export default { ...@@ -91,7 +94,7 @@ export default {
this.getMinuteList(days, hours) this.getMinuteList(days, hours)
}, },
getMinuteList (days, hours) { getMinuteList(days, hours) {
const minutes = []; const minutes = [];
const today = new Date() const today = new Date()
for (let minute = 0; minute < today.getMinutes() + 1; minute++) { for (let minute = 0; minute < today.getMinutes() + 1; minute++) {
...@@ -123,7 +126,7 @@ export default { ...@@ -123,7 +126,7 @@ export default {
}, 100) }, 100)
this.$emit('onComplete') this.$emit('onComplete')
}, },
changeDate (e) { changeDate(e) {
const { days } = this const { days } = this
const { value } = e.detail const { value } = e.detail
const index = value[0] const index = value[0]
...@@ -133,7 +136,7 @@ export default { ...@@ -133,7 +136,7 @@ export default {
const timeType = this.currentDate != today ? 1 : 0 const timeType = this.currentDate != today ? 1 : 0
this.getAllTimeOrPartTime(timeType) this.getAllTimeOrPartTime(timeType)
}, },
changeHour (e) { changeHour(e) {
const { hours, days, dateValues } = this const { hours, days, dateValues } = this
const { value } = e.detail const { value } = e.detail
const index = value[0] const index = value[0]
...@@ -146,13 +149,13 @@ export default { ...@@ -146,13 +149,13 @@ export default {
const timeType = this.currentDate != today ? 1 : 0 const timeType = this.currentDate != today ? 1 : 0
this.getAllTimeOrPartTimeMinute(timeType) this.getAllTimeOrPartTimeMinute(timeType)
}, },
changeMinutes (e) { changeMinutes(e) {
const { minutes } = this const { minutes } = this
const { value } = e.detail const { value } = e.detail
const index = value[0] const index = value[0]
this.currentMinute = minutes[index] this.currentMinute = minutes[index]
}, },
getAllTimeOrPartTime (type) { getAllTimeOrPartTime(type) {
const today = new Date() const today = new Date()
const hours = [], minutes = [], hour_len = type == 1 ? 24 : today.getHours() + 1, minute_len = type == 1 ? 60 : today.getMinutes() + 1; 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++) { for (let hour = 0; hour < hour_len; hour++) {
...@@ -164,7 +167,7 @@ export default { ...@@ -164,7 +167,7 @@ export default {
this.hours = hours this.hours = hours
this.minutes = minutes this.minutes = minutes
}, },
getAllTimeOrPartTimeMinute (type) { getAllTimeOrPartTimeMinute(type) {
const today = new Date() const today = new Date()
const currentHour = today.getHours() const currentHour = today.getHours()
const timeType = this.currentHour != currentHour ? 1 : 0 const timeType = this.currentHour != currentHour ? 1 : 0
...@@ -174,9 +177,9 @@ export default { ...@@ -174,9 +177,9 @@ export default {
} }
this.minutes = minutes this.minutes = minutes
}, },
getResult () { getResult() {
let str = '' let str = ''
str = this.currentDate + ' ' + this.currentHour + ':' + this.currentMinute str = this.currentDate + ' ' + this.currentHour + ':' + this.currentMinute
// + ":00" // + ":00"
const result = dayjs(str).format('YYYY-MM-DD HH:mm') const result = dayjs(str).format('YYYY-MM-DD HH:mm')
return result return result
...@@ -190,9 +193,11 @@ export default { ...@@ -190,9 +193,11 @@ export default {
height: 100%; height: 100%;
display: grid; display: grid;
grid-template-columns: repeat(3, 1fr); grid-template-columns: repeat(3, 1fr);
.picker { .picker {
height: 100%; height: 100%;
text-align: center; text-align: center;
.view { .view {
display: flex; display: flex;
align-items: center; align-items: center;
...@@ -201,6 +206,7 @@ export default { ...@@ -201,6 +206,7 @@ export default {
height: toRpx(72); height: toRpx(72);
} }
} }
/deep/.indicator { /deep/.indicator {
height: toRpx(72); height: toRpx(72);
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!