Source: lib/date.js

'use strict';

/* 日期操作 */
/** @module date */

/**
 * formatTime 获取格式化时间字符串
 * @param {String} date - 时间戳字符串或者Date对象
 * @param {String} [fmt] - 转换规则, 如yyyy-MM-dd HH:mm:ss SS
 * @return {String} 格式化后的时间字符串
 */
function formatTime(date,fmt = 'yyyy-MM-dd HH:mm:ss') {
    if(typeof date == 'string'){
        date = new Date(parseInt(date));
    }
    let o = {
        'M+': date.getMonth() + 1, // 月份
        'd+': date.getDate(), // 日
        'H+': date.getHours(), // 小时
        'm+': date.getMinutes(), // 分
        's+': date.getSeconds(), // 秒
        'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
        'S': date.getMilliseconds() // 毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
    for (let k in o) { if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length))); }
    return fmt;
}

export {
    formatTime
};