载入天数...载入时分秒...

Javascript常用工具类封装

  • 获取当前时间yyyy-MM-dd HH:mm:ssyyyy-MM-dd
    常用版
function getFormatDate(isCompleteDate = true) {
    //   获取带时分秒的,不带的记得加false
    {
        let t = new Date(),
            y = t.getFullYear(),
            m = t.getMonth() + 1,
            a = t.getDate(),
            h = t.getHours(),
            min = t.getMinutes(),
            s = t.getSeconds(),
            str = (1 <= m && m <= 9 && (m = "0" + m),
                0 <= a && a <= 9 && (a = "0" + a),
                0 <= h && h <= 9 && (h = "0" + h),
                0 <= min && min <= 9 && (min = "0" + min),
                0 <= s && s <= 9 && (s = "0" + s),
                y + "-" + m + "-" + a);
        if (isCompleteDate) {
            str = str + " " + h + ":" + min + ":" + s;
        }
        return str;
    }
}

精简版

function getFormatDate(isCompleteDate = true) {
    {
        let t = new Date(),
            y = t.getFullYear(),
            m = t.getMonth() + 1,
            d = t.getDate(),
            h = t.getHours(),
            min = t.getMinutes(),
            s = t.getSeconds();
        return 1 <= m && m <= 9 && (m = "0" + m),
            0 <= d && d <= 9 && (d = "0" + d),
            0 <= h && h <= 9 && (h = "0" + h),
            0 <= min && min <= 9 && (min = "0" + min),
            0 <= s && s <= 9 && (s = "0" + s), isCompleteDate ?
            (y + "-" + m + "-" + d + ' ' + h + ':' + min + ':' + s) : y + "-" + m + "-" + d;
    }
}
posted @ 2021-10-09 17:53  旧信  阅读(90)  评论(0编辑  收藏  举报