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;
    }
}
上一篇:springboot~对@RequestParam中Date参数的适配


下一篇:将String类型的日期格式为yyyyMMdd转化为date类型的yyyy-MM-dd格式