编程语言
首页 > 编程语言> > Javascript常用工具类封装

Javascript常用工具类封装

作者:互联网

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;
    }
}

标签:getSeconds,封装,getMinutes,min,getHours,Javascript,yyyy,isCompleteDate,常用工具
来源: https://www.cnblogs.com/lylhqy/p/15387008.html