flo-utils
Version:
31 lines (27 loc) • 704 B
JavaScript
/**
* @public
* @name formatDate
* @param {date}
* @param {string} rule 格式
* @return {string}
* @description 时间格式化
* @example
*
* formatDate(new Date(), 'YYYY-MM-DD HH:mm:ss');
* => '2019-01-02 20:26:00' // 举例
*
* formatDate('20190101');
* => '20190101'
*/
import moment from 'moment';
var formatDate = function formatDate(date) {
var rule = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'YYYY-MM-DD';
if (!date) return '';
var ndate = date;
if (typeof ndate === 'string') {
ndate = ndate.trim();
}
var text = moment(ndate).format(rule);
return text === 'Invalid date' ? ndate : text;
};
export default formatDate;