UNPKG

dtable-utils

Version:

dtable common utils

27 lines (24 loc) 835 B
import { DateUtils } from '../date.js'; import { getFloatNumber } from './number.js'; /** * Parse string to date * @param {string} text * @param {string} format default as "YYYY-MM-DD" * @returns date, string */ var formatTextToDate = function formatTextToDate(text) { var format = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'YYYY-MM-DD'; if (typeof text !== 'string' || !text.trim()) return null; var isAllNumber = /^[0-9]+$/.test(text); var dateObj = {}; if (isAllNumber) { dateObj = new Date(getFloatNumber(text)); } else { dateObj = DateUtils.parseDateWithFormat(text, format); } if (format.indexOf('HH:mm') < 0) { return DateUtils.format(dateObj, 'YYYY-MM-DD') || null; } return DateUtils.format(dateObj, 'YYYY-MM-DD HH:MM') || null; }; export { formatTextToDate };