快捷选择日期

This commit is contained in:
郁万成 2024-10-29 09:47:13 +08:00
parent 6abc588e4d
commit bd1246782d

View File

@ -81,10 +81,19 @@ const adminInfo = useAdminInfo()
const timeRange = ref([])
const shortcuts = [
{
text: '今天',
value: () => {
const end = new Date()
const start = new Date()
return [start, end]
},
},
{
text: '昨天',
value: () => {
const end = new Date()
end.setDate(end.getDate() - 1)
const start = new Date()
start.setDate(start.getDate() - 1)
return [start, end]
@ -95,26 +104,20 @@ const shortcuts = [
value: () => {
const end = new Date()
const start = new Date()
start.setDate(start.getDate() - 3)
start.setDate(start.getDate() - 2)
return [start, end]
},
},
{
text: '周',
text: '周',
value: () => {
const end = new Date()
const start = new Date()
start.setDate(start.getDate() - 7)
return [start, end]
return getDateRange('week')
},
},
{
text: '上个月',
text: '月',
value: () => {
const end = new Date()
const start = new Date()
start.setMonth(start.getMonth() - 1)
return [start, end]
return getDateRange('month')
},
},
]
@ -201,25 +204,28 @@ const okSubmit = (val) => {
console.log(val)
getalarmsList()
}
const getTodayAndYesterday = () => {
const getDateRange = (type: 'week' | 'month') => {
const today = new Date()
const yesterday = new Date()
yesterday.setDate(today.getDate() - 1)
const formatDate = (date) => {
const year = date.getFullYear()
const month = String(date.getMonth() + 1).padStart(2, '0')
const day = String(date.getDate()).padStart(2, '0')
return `${year}-${month}-${day}`
if (type === 'week') {
const dayOfWeek = today.getDay()
const startOfWeek = new Date(today)
startOfWeek.setDate(today.getDate() - dayOfWeek + (dayOfWeek === 0 ? -6 : 1))
startOfWeek.setHours(0, 0, 0, 0)
const endOfWeek = new Date(startOfWeek)
endOfWeek.setDate(startOfWeek.getDate() + 6)
endOfWeek.setHours(23, 59, 59, 999)
return [startOfWeek, endOfWeek]
}
return {
today: formatDate(today),
yesterday: formatDate(yesterday),
if (type === 'month') {
const startOfMonth = new Date(today.getFullYear(), today.getMonth(), 1)
startOfMonth.setHours(0, 0, 0, 0)
const endOfMonth = new Date(today.getFullYear(), today.getMonth() + 1, 0)
endOfMonth.setHours(23, 59, 59, 999)
return [startOfMonth, endOfMonth]
}
}
onMounted(() => {
// timeRange[0] = getTodayAndYesterday().yesterday
// timeRange[1] = getTodayAndYesterday().today
equipList({
// orgId: adminInfo.orgid,
objectType: 10002,