feat: 时间组件参数改为时间戳

This commit is contained in:
fit2cloud-chenyw 2021-05-21 12:19:32 +08:00
parent c38bc7f3d8
commit 955a0ec6bc
2 changed files with 55 additions and 26 deletions

View File

@ -13,7 +13,7 @@
</template>
<script>
import { dateFormat } from '@/utils'
import { timeSection } from '@/utils'
export default {
props: {
@ -29,7 +29,7 @@ export default {
data() {
return {
options: null,
operator: 'eq',
operator: 'in',
values: null
}
},
@ -50,37 +50,26 @@ export default {
this.inDraw && this.$store.dispatch('conditions/add', param)
},
dateChange(value) {
// const nvalue = dateFormat(value, this.getFormat())
// console.log(nvalue)
this.setCondition()
},
formatValues(values) {
if (!values || values.length === 0) {
return []
}
return values.map(value => dateFormat(value, this.getFormat()))
},
getFormat() {
let format = 'yyyy'
switch (this.options.attrs.type) {
case 'year':
format = 'yyyy'
break
case 'month':
format = 'yyyy-MM'
break
case 'date':
format = 'yyyy-MM-dd'
break
case 'daterange':
format = 'yyyy-MM-dd'
this.operator = 'in'
break
default:
format = 'yyyy'
break
if (this.options.attrs.type === 'daterange') {
if (values.length !== 2) {
return null
}
let start = values[0]
let end = values[1]
start = timeSection(start, 'date')[0]
end = timeSection(end, 'date')[1]
const results = [start, end]
return results
} else {
const value = values[0]
return timeSection(value, this.options.attrs.type)
}
return format
}
}
}

View File

@ -1,7 +1,47 @@
/**
* Created by PanJiaChen on 16/11/18.
*/
export function timeSection(date, type) {
if (!date) {
return null
}
const timeRanger = new Array(2)
date.setHours(0)
date.setMinutes(0)
date.setSeconds(0)
date.setMilliseconds(0)
const end = new Date(date)
if (type === 'year') {
date.setDate(1)
date.setMonth(0)
end.setFullYear(date.getFullYear() + 1)
timeRanger[1] = end.getTime() - 1
}
if (type === 'month') {
date.setDate(1)
const currentMonth = date.getMonth()
if (currentMonth === 11) {
end.setFullYear(date.getFullYear() + 1)
end.setMonth(0)
} else {
end.setMonth(date.getMonth() + 1)
}
timeRanger[1] = end.getTime() - 1
}
if (type === 'date') {
end.setHours(23)
end.setMinutes(59)
end.setSeconds(59)
end.setMilliseconds(999)
timeRanger[1] = end.getTime()
}
timeRanger[0] = date.getTime()
return timeRanger
}
export function dateFormat(date, fmt) {
let ret
const opt = {