Skip to content

Commit

Permalink
fix(date-picker): 修复在时间戳模式下panel会显示Invalid value的问题 (Tencent#1268)
Browse files Browse the repository at this point in the history
* fix(date-picker): 修复在时间戳模式下panel会显示Invalid value的问题

* feat(date-picker): 调整了代码结构,简化部分if判断环节
  • Loading branch information
Ericleungs authored and oljc committed May 9, 2023
1 parent 17d2327 commit 4aa31ea
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions js/date-picker/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,18 +206,18 @@ export function calcFormatTime(time: string, timeFormat: string) {
}

// 格式化时间
export function formatTime(value: DateValue | DateValue[], format:string, timeFormat: string, defaultTime: string | string[]) {
let result;

if (Array.isArray(value)) {
// eslint-disable-next-line no-param-reassign
if (!Array.isArray(defaultTime)) defaultTime = [defaultTime, defaultTime];
result = value.map((v, i) => (v ? dayjs(v, format).format(timeFormat) : calcFormatTime(defaultTime[i], timeFormat)));
result = result.length ? result : defaultTime.map((t) => calcFormatTime(t, timeFormat));
} else {
result = value ? dayjs(value, format).format(timeFormat) : calcFormatTime(defaultTime as string, timeFormat);
}
return result;
export function formatTime(value: DateValue | DateValue[], format: string, timeFormat: string, defaultTime: string | string[]) {
// 无论参数是不是数组,统一转成数组处理
let result = Array.isArray(value) ? value : [value];
// eslint-disable-next-line no-param-reassign
defaultTime = Array.isArray(defaultTime) ? defaultTime : [defaultTime, defaultTime];
result = result.map((v, i) => {
// string格式需要用format去解析,其他诸如Date、time-stamp格式则直接dayjs
if (v) return dayjs(v, typeof v === 'string' ? format : undefined).format(timeFormat);
return calcFormatTime(defaultTime[i], timeFormat);
});
// value是数组就输出数组,不是数组就输出第一个即可
return Array.isArray(value) ? result : result?.[0];
}

// 根据不同 mode 给出格式化默认值
Expand Down

0 comments on commit 4aa31ea

Please sign in to comment.