mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
chore: improve calendar utils (#8521)
This commit is contained in:
parent
35edb72b5c
commit
9381640d2e
src/calendar
@ -14,8 +14,8 @@ import {
|
||||
t,
|
||||
bem,
|
||||
name,
|
||||
copyDate,
|
||||
copyDates,
|
||||
cloneDate,
|
||||
cloneDates,
|
||||
getPrevDay,
|
||||
getNextDay,
|
||||
compareDay,
|
||||
@ -341,12 +341,12 @@ export default defineComponent({
|
||||
return true;
|
||||
};
|
||||
|
||||
const onConfirm = () => emit('confirm', copyDates(state.currentDate));
|
||||
const onConfirm = () => emit('confirm', cloneDates(state.currentDate));
|
||||
|
||||
const select = (date: Date | Date[], complete?: boolean) => {
|
||||
const setCurrentDate = (date: Date | Date[]) => {
|
||||
state.currentDate = date;
|
||||
emit('select', copyDates(state.currentDate));
|
||||
emit('select', cloneDates(state.currentDate));
|
||||
};
|
||||
|
||||
if (complete && props.type === 'range') {
|
||||
@ -422,7 +422,7 @@ export default defineComponent({
|
||||
|
||||
if (selected) {
|
||||
const [unselectedDate] = currentDate.splice(selectedIndex, 1);
|
||||
emit('unselect', copyDate(unselectedDate));
|
||||
emit('unselect', cloneDate(unselectedDate));
|
||||
} else if (props.maxRange && currentDate.length >= props.maxRange) {
|
||||
Toast(props.rangePrompt || t('rangePrompt', props.maxRange));
|
||||
} else {
|
||||
|
@ -11,10 +11,10 @@ export function formatMonthTitle(date: Date) {
|
||||
export function compareMonth(date1: Date, date2: Date) {
|
||||
const year1 = date1.getFullYear();
|
||||
const year2 = date2.getFullYear();
|
||||
const month1 = date1.getMonth();
|
||||
const month2 = date2.getMonth();
|
||||
|
||||
if (year1 === year2) {
|
||||
const month1 = date1.getMonth();
|
||||
const month2 = date2.getMonth();
|
||||
return month1 === month2 ? 0 : month1 > month2 ? 1 : -1;
|
||||
}
|
||||
|
||||
@ -27,48 +27,28 @@ export function compareDay(day1: Date, day2: Date) {
|
||||
if (compareMonthResult === 0) {
|
||||
const date1 = day1.getDate();
|
||||
const date2 = day2.getDate();
|
||||
|
||||
return date1 === date2 ? 0 : date1 > date2 ? 1 : -1;
|
||||
}
|
||||
|
||||
return compareMonthResult;
|
||||
}
|
||||
|
||||
export const cloneDate = (date: Date) => new Date(date);
|
||||
|
||||
export const cloneDates = (dates: Date | Date[]) =>
|
||||
Array.isArray(dates) ? dates.map(cloneDate) : cloneDate(dates);
|
||||
|
||||
export function getDayByOffset(date: Date, offset: number) {
|
||||
date = new Date(date);
|
||||
date.setDate(date.getDate() + offset);
|
||||
|
||||
return date;
|
||||
const cloned = cloneDate(date);
|
||||
cloned.setDate(cloned.getDate() + offset);
|
||||
return cloned;
|
||||
}
|
||||
|
||||
export function getPrevDay(date: Date) {
|
||||
return getDayByOffset(date, -1);
|
||||
}
|
||||
|
||||
export function getNextDay(date: Date) {
|
||||
return getDayByOffset(date, 1);
|
||||
}
|
||||
export const getPrevDay = (date: Date) => getDayByOffset(date, -1);
|
||||
export const getNextDay = (date: Date) => getDayByOffset(date, 1);
|
||||
|
||||
export function calcDateNum(date: [Date, Date]) {
|
||||
const day1 = date[0].getTime();
|
||||
const day2 = date[1].getTime();
|
||||
return (day2 - day1) / (1000 * 60 * 60 * 24) + 1;
|
||||
}
|
||||
|
||||
export function copyDate(dates: Date) {
|
||||
return new Date(dates);
|
||||
}
|
||||
|
||||
export function copyDates(dates: Date | Date[]) {
|
||||
if (Array.isArray(dates)) {
|
||||
return dates.map((date) => {
|
||||
if (date === null) {
|
||||
return date;
|
||||
}
|
||||
|
||||
return copyDate(date);
|
||||
});
|
||||
}
|
||||
|
||||
return copyDate(dates);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user