Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 33x 7x 2x 5x 33x 2x 33x | import { parseISO, toDate } from 'date-fns'
import { isNumeric } from '../utils'
/**
* 增强版的 toDate,支持:
* - 秒时间戳、毫秒时间戳;
* - Date 实例;
* - 符合 ISO 标准的时间字符串。
*
* @param value 要转换的值
* @returns 返回转换后的 Date 实例
*/
export function anyToDate(value: string | number | Date): Date {
if (typeof value === 'string') {
if (isNumeric(value)) {
value = Number(value)
} else {
value = parseISO(value)
}
}
if (typeof value === 'number' && String(value).length === 10) {
value *= 1000
}
return toDate(value)
}
|