1. 现有公共方法 时间戳--'YYYY-MM-DD'
export const formatTime = time => {
if (!['-', undefined, '', 0].includes(time)) {
return dayjs(time * 1000).format('YYYY-MM-DD');
}
return '-';
};
2. 增加'YYYY-MM-DD HH:mm:ss'场景(不影响原代码功能及使用)
第一次优化:
export const formatTime = (time, val) => {
if (!['-', undefined, '', 0].includes(time)) {
if (val) {
return dayjs(time * 1000).format('YYYY-MM-DD');
} else {
return dayjs(time * 1000).format('YYYY-MM-DD HH:mm:ss');
}
}
return '-';
};
formatTime(time, true);
问题:1)代码简洁性; 2)考虑可扩展性--可能存在其他时间格式需求。
二次优化:
export const formatTime = (time, val = 'YYYY-MM-DD') => {
if (!['-', undefined, '', 0].includes(time)) {
return dayjs(time * 1000).format(val);
}
return '-';
};
formatTime(time, 'YYYY-MM-DD HH:mm:ss');
设置默认时间格式,根据场景需求进行传参