Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions js/date-picker/format.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { isString } from 'lodash-es';

import dayjs from 'dayjs';
import isoWeeksInYear from 'dayjs/plugin/isoWeeksInYear';
import isLeapYear from 'dayjs/plugin/isLeapYear';
Expand All @@ -25,30 +26,29 @@ export function extractTimeFormat(dateFormat: string = '') {
export function parseToDayjs(
value: string | Date | number,
format: string,
timeOfDay?: string,
dayjsLocale?: string,
timeOfDay?: string
) {
if (value === '' || value === null) return dayjs();

let dateText = value;
// format week
if (/[w|W]/g.test(format)) {
if (!isString(dateText)) {
dateText = dayjs(dateText).locale(dayjsLocale || 'zh-cn').format(format) as string;
dateText = dayjs(dateText).format(format) as string;
}

const yearStr = dateText.split(/[-/.\s]/)[0];
const weekStr = dateText.split(/[-/.\s]/)[1];
const weekFormatStr = format.split(/[-/.\s]/)[1];

let firstWeek = dayjs(yearStr, 'YYYY').locale(dayjsLocale || 'zh-cn').startOf('year');
let firstWeek = dayjs(yearStr, 'YYYY').startOf('year');
// 第一周ISO定义: 本年度第一个星期四所在的星期
// 如果第一年第一天在星期四后, 直接跳到下一周, 下一周必定是第一周
// 否则本周即为第一周
if (firstWeek.day() > 4 || firstWeek.day() === 0) firstWeek = firstWeek.add(1, 'week');

// 一年有52或者53周, 引入IsoWeeksInYear辅助查询
const weekCounts = dayjs(yearStr, 'YYYY').locale(dayjsLocale || 'zh-cn').isoWeeksInYear();
const weekCounts = dayjs(yearStr, 'YYYY').isoWeeksInYear();
for (let i = 0; i <= weekCounts; i += 1) {
let nextWeek = firstWeek.add(i, 'week');
// 重置为周的第一天
Expand All @@ -62,7 +62,7 @@ export function parseToDayjs(
// format quarter
if (/Q/g.test(format)) {
if (!isString(dateText)) {
dateText = dayjs(dateText).locale(dayjsLocale || 'zh-cn').format(format) as string;
dateText = dayjs(dateText).format(format) as string;
}

const yearStr = dateText.split(/[-/.\s]/)[0];
Expand Down Expand Up @@ -95,19 +95,17 @@ export function parseToDayjs(
function formatRange({
newDate,
format,
dayjsLocale,
targetFormat,
autoSwap,
}: {
newDate: any;
format: string;
dayjsLocale?: string;
targetFormat?: string;
autoSwap?: boolean;
}) {
if (!newDate || !Array.isArray(newDate)) return [];

let dayjsDateList = newDate.map((d) => d && parseToDayjs(d, format).locale(dayjsLocale));
let dayjsDateList = newDate.map((d) => d && parseToDayjs(d, format));

// 保证后面的时间大于前面的时间
if (
Expand Down Expand Up @@ -142,16 +140,14 @@ function formatSingle({
newDate,
format,
targetFormat,
dayjsLocale,
}: {
newDate: any;
format: string;
targetFormat?: string;
dayjsLocale?: string;
}) {
if (!newDate) return '';

const dayJsDate = parseToDayjs(newDate, format).locale(dayjsLocale);
const dayJsDate = parseToDayjs(newDate, format);

// 格式化失败提示
if (!dayJsDate.isValid()) {
Expand Down Expand Up @@ -189,16 +185,24 @@ export function formatDate(
{
format,
targetFormat,
dayjsLocale = 'zh-cn',
autoSwap,
}: { format: string; dayjsLocale?: string, targetFormat?: string; autoSwap?: boolean }
}: {
format: string;
targetFormat?: string;
autoSwap?: boolean;
}
) {
let result;

if (Array.isArray(newDate)) {
result = formatRange({ newDate, format, dayjsLocale, targetFormat, autoSwap });
result = formatRange({
newDate,
format,
targetFormat,
autoSwap,
});
} else {
result = formatSingle({ newDate, format, dayjsLocale, targetFormat });
result = formatSingle({ newDate, format, targetFormat });
}

return result;
Expand Down
46 changes: 23 additions & 23 deletions js/date-picker/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { isFunction, chunk } from 'lodash-es';
import { chunk, isFunction } from 'lodash-es';

import dayjs from 'dayjs';
import dayJsIsBetween from 'dayjs/plugin/isBetween';
import weekOfYear from 'dayjs/plugin/weekOfYear';
Expand Down Expand Up @@ -71,8 +72,10 @@ function isSameMonth(date1: Date, date2: Date): boolean {
return isSameYear(date1, date2) && date1.getMonth() === date2.getMonth();
}

function isSameWeek(date1: Date, date2: Date, dayjsLocale = 'zh-cn'): boolean {
return isSameMonth(date1, date2) && dayjs(date1).locale(dayjsLocale).week() === dayjs(date2).locale(dayjsLocale).week();
function isSameWeek(date1: Date, date2: Date): boolean {
return (
isSameMonth(date1, date2) && dayjs(date1).week() === dayjs(date2).week()
);
}

function isSameDate(date1: Date, date2: Date): boolean {
Expand Down Expand Up @@ -101,15 +104,15 @@ function compareAsc(date1: { getTime: () => any }, date2: Date): number {
* @param {String} type 比较类型,默认比较到『日』 date|month|year
* @returns {Boolean}
*/
export function isSame(date1: Date, date2: Date, type = 'date', dayjsLocale = 'zh-cn'): boolean {
export function isSame(date1: Date, date2: Date, type = 'date'): boolean {
const func = {
isSameYear,
isSameQuarter,
isSameMonth,
isSameWeek,
isSameDate,
} as Record<string, (date1: Date, date2: Date, dayjsLocale?: string) => boolean>;
return func[`isSame${firstUpperCase(type)}`](date1, date2, dayjsLocale);
} as Record<string, (date1: Date, date2: Date) => boolean>;
return func[`isSame${firstUpperCase(type)}`](date1, date2);
}

export function outOfRanges(d: Date, min: any, max: any) {
Expand Down Expand Up @@ -200,7 +203,6 @@ export interface OptionsType {
minDate: Date;
maxDate: Date;
showWeekOfYear?: Boolean;
dayjsLocale?: string;
monthLocal?: string[];
quarterLocal?: string[];
cancelRangeSelectLimit?: boolean;
Expand All @@ -214,9 +216,8 @@ export function getWeeks(
disableDate = () => false,
minDate,
maxDate,
dayjsLocale = 'zh-cn',
cancelRangeSelectLimit = false,
}: OptionsType,
}: OptionsType
) {
const prependDay = getFirstDayOfMonth({ year, month });
const appendDay = getLastDayOfMonth({ year, month });
Expand All @@ -230,13 +231,14 @@ export function getWeeks(
text: i,
active: false,
value: currentDay,
disabled: (isFunction(disableDate) && disableDate(currentDay))
disabled:
(isFunction(disableDate) && disableDate(currentDay))
|| (!cancelRangeSelectLimit && outOfRanges(currentDay, minDate, maxDate)),
now: isSame(today, currentDay),
firstDayOfMonth: i === 1,
lastDayOfMonth: i === maxDays,
type: 'current-month',
dayjsObj: dayjs(currentDay).locale(dayjsLocale),
dayjsObj: dayjs(currentDay),
});
}

Expand All @@ -250,7 +252,7 @@ export function getWeeks(
disabled: (isFunction(disableDate) && disableDate(prependDay)) || (!cancelRangeSelectLimit && outOfRanges(prependDay, minDate, maxDate)),
additional: true, // 非当前月
type: 'prev-month',
dayjsObj: dayjs(prependDay).locale(dayjsLocale),
dayjsObj: dayjs(prependDay),
});
prependDay.setDate(prependDay.getDate() - 1);
if (prependDay.getDay() === Math.abs(firstDayOfWeek + 6) % 7) break;
Expand All @@ -267,7 +269,7 @@ export function getWeeks(
disabled: (isFunction(disableDate) && disableDate(appendDay)) || (!cancelRangeSelectLimit && outOfRanges(appendDay, minDate, maxDate)),
additional: true, // 非当前月
type: 'next-month',
dayjsObj: dayjs(appendDay).locale(dayjsLocale),
dayjsObj: dayjs(appendDay),
});
}

Expand All @@ -279,8 +281,8 @@ export function getWeeks(
...d[0],
active: false,
value: d[0].value,
text: dayjs(d[0].value).locale(dayjsLocale).week(),
dayjsObj: dayjs(d[0].value).locale(dayjsLocale),
text: dayjs(d[0].value).week(),
dayjsObj: dayjs(d[0].value),
});
});
}
Expand All @@ -295,9 +297,8 @@ export function getQuarters(
minDate,
maxDate,
quarterLocal,
dayjsLocale = 'zh-cn',
cancelRangeSelectLimit = false,
}: OptionsType,
}: OptionsType
) {
const quarterArr = [];
const today = getToday();
Expand All @@ -311,7 +312,7 @@ export function getQuarters(
disabled: (isFunction(disableDate) && disableDate(date)) || (!cancelRangeSelectLimit && outOfRanges(date, minDate, maxDate)),
active: false,
text: quarterLocal[i - 1],
dayjsObj: dayjs(date).locale(dayjsLocale),
dayjsObj: dayjs(date),
});
}

Expand All @@ -324,9 +325,8 @@ export function getYears(
disableDate = () => false,
minDate,
maxDate,
dayjsLocale = 'zh-cn',
cancelRangeSelectLimit = false,
}: OptionsType,
}: OptionsType
) {
const startYear = parseInt((year / 10).toString(), 10) * 10;
const endYear = startYear + 9;
Expand All @@ -344,7 +344,7 @@ export function getYears(
disabled: (isFunction(disableDate) && disableDate(date)) || (!cancelRangeSelectLimit && outOfRanges(date, minDate, maxDate)),
active: false,
text: `${date.getFullYear()}`,
dayjsObj: dayjs(date).locale(dayjsLocale),
dayjsObj: dayjs(date),
});
}

Expand All @@ -353,7 +353,7 @@ export function getYears(

export function getMonths(year: number, params: OptionsType) {
const {
disableDate = () => false, minDate, maxDate, monthLocal, dayjsLocale = 'zh-cn', cancelRangeSelectLimit = false,
disableDate = () => false, minDate, maxDate, monthLocal, cancelRangeSelectLimit = false,
} = params;
const MonthArr = [];
const today = getToday();
Expand All @@ -367,7 +367,7 @@ export function getMonths(year: number, params: OptionsType) {
disabled: (isFunction(disableDate) && disableDate(date)) || (!cancelRangeSelectLimit && outOfRanges(date, minDate, maxDate)),
active: false,
text: monthLocal[date.getMonth()], // `${date.getMonth() + 1} ${monthText || '月'}`,
dayjsObj: dayjs(date).locale(dayjsLocale),
dayjsObj: dayjs(date),
});
}

Expand Down