Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
get (target, key) {
if (dayjs.isDayjs(target)) {
// 是 Dayjs 对象,正常返回
return target[key]
} else {
// 不是 Dayjs 对象
if (dayjs(target).isValid()) {
// 尝试帮用户解析成 Dayjs 对象
return dayjs(target)[key]
} else {
// 无法解析
return function () {
return '无效日期'
}
}
}
},
set (target, key, value) {
it('Is a Date', () => {
expect(moment.isDate(new Date())).toBeTruthy();
expect(new Date() instanceof Date).toBeTruthy();
expect(date.isDate(new Date())).toBeTruthy();
expect(dayjs.isDayjs(dayjs())).toBeTruthy();
expect(DateTime.local().isValid).toBeTruthy();
});
});
export function isAfterDate (date, afterDate, type = 'day') {
if (type === 'year') {
return Boolean(afterDate) && date > dayjs(afterDate, 'YYYY-MM-DD').get(type);
}
const selectedDate = dayjs.isDayjs(date) ? date : dayjs(date).startOf('day');
return Boolean(afterDate) && selectedDate.isAfter(dayjs(afterDate).startOf('day'), type);
}
export function generateDateRange (startDate, endDate, interval = 'day') {
const start = dayjs.isDayjs(startDate) ? startDate : dayjs(startDate);
const end = dayjs.isDayjs(endDate) ? endDate : dayjs(endDate);
const diffBetweenDates = end.diff(start, interval);
return [...Array(diffBetweenDates + 1).keys()].map(i => start.add(i, interval));
}
export function generateDateRange (startDate, endDate, interval = 'day') {
const start = dayjs.isDayjs(startDate) ? startDate : dayjs(startDate);
const end = dayjs.isDayjs(endDate) ? endDate : dayjs(endDate);
const diffBetweenDates = end.diff(start, interval);
return [...Array(diffBetweenDates + 1).keys()].map(i => start.add(i, interval));
}
export function isBeforeDate (date, beforeDate, type = 'day') {
if (type === 'year') {
return Boolean(beforeDate) && date < dayjs(beforeDate, 'YYYY-MM-DD').get(type);
}
const selectedDate = dayjs.isDayjs(date) ? date : dayjs(date).startOf('day');
return Boolean(beforeDate) && selectedDate.isBefore(dayjs(beforeDate).startOf('day'), type);
}