![](https://img-blog.csdnimg.cn/c224ecf7c0444a4cabc2c09afd72e4fe.png)
// 日期范围的处理
const startDate = new Date()
const endDate = new Date()
endDate.setDate(startDate.getDate() + 1) //加一天
const startDateStr = computed(() => formatMonthDay(startDate.value))
const endDateStr = computed(() => formatMonthDay(endDate.value))
const stayCount = ref(getDiffDays(startDate.value, endDate.value))
const showCalendar = ref(false)
const onConfirm = value => {
// 1.设置日期
const selectStartDate = value[0]
const selectEndDate = value[1]
mainStore.startDate = selectStartDate
mainStore.endDate = selectEndDate
// stayCount.value 计算天数
stayCount.value = getDiffDays(selectStartDate, selectEndDate)
// 2.隐藏日历
showCalendar.value = false
}
import dayjs from 'dayjs'
export function formatMonthDay(date, formatStr = "MM月DD日") {
return dayjs(date).format(formatStr)
}
export function getDiffDays(startDate, endDate) {
return dayjs(endDate).diff(startDate, "day")
}
Difference · Day.js