Commit 450b9d2d by 李伟康

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

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