UNPKG

@planjs/utils

Version:

🔧 Common tools collection

39 lines (31 loc) 861 B
import isDigit from '../is/is-digit'; import isDate from '../is/is-Date'; import isString from '../is/is-String'; import between from '../number/between'; import throwError from '../throw-error'; var invalidMsg = 'Invalid Date'; /** * 时间戳,字符串,转换成 Date * @param input * @throws Invalid Date * @category Date */ function toDate(input) { if (isDate(input)) return input; var date; if (isDigit(input)) { var len = String(Math.trunc(input)).length; if (!between(len, 10, 14)) { throwError(invalidMsg); } input = input * (len === 10 ? 1000 : 1); date = new Date(Math.floor(input)); } else if (isString(input)) { date = new Date(input.replace(/[^\d\s:]+/g, '/')); } // Invalid Date if (!date || isNaN(date.getTime())) { throwError(invalidMsg); } return date; } export default toDate;