快捷选择日期

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