new Date()日期格式处理

https://blog.csdn.net/zwt_guiji/article/details/80258944

绩效项目中使用的日期格式化封装:

    $.dateChange = {
        formatDay: function (date) {
            if (date != null) {
                date = new Date(date);
                var y = date.getFullYear();
                var m = date.getMonth() + 1;
                m = m < 10 ? '0' + m : m;
                var d = date.getDate();
                d = d < 10 ? ('0' + d) : d;
                return y + '-' + m + '-' + d;
            }
            return "";
        },
        formatSecond: function (date) {
            date = new Date(date);
            var y = date.getFullYear();
            var m = date.getMonth() + 1;
            m = m < 10 ? '0' + m : m;
            var d = date.getDate();
            d = d < 10 ? ('0' + d) : d;
            var h = date.getHours();
            h = h < 10 ? '0' + h : h;
            var min = date.getMinutes();
            min = min < 10 ? '0' + min : min;
            var s = date.getSeconds();
            s = s < 10 ? '0' + s : s;
            return y + '-' + m + '-' + d + '&ensp;' + h + ':' + min + ':' + s;
        },
        /**
         * 格式化时间对象----功能可扩展
         * @param date [Date] 要格式化的时间对象
         * @param format [String]格式化的形式  yyyy-mm-dd hh:MM:ss   yyyy-m-d h:M:s
         * @returns {string} 格式化的结果字符串
         */
        formatDate: function (date, format) {
            var year = date.getFullYear(),
                month = date.getMonth() + 1,
                day = date.getDate(),
                hour = date.getHours(),
                minute = date.getMinutes(),
                second = date.getSeconds();
            // 两位
            format = format
                .replace(/yyyy/i, year)
                .replace(/mm/i, $.string.formatNum2(month))
                .replace(/dd/i, $.string.formatNum2(day))
                .replace(/hh/i, $.string.formatNum2(hour))
                .replace(/MM/i, $.string.formatNum2(minute))
                .replace(/ss/i, $.string.formatNum2(second));
            //一位
            format = format
                .replace(/m/i, month)
                .replace(/d/i, day)
                .replace(/h/i, hour)
                .replace(/M/i, minute)
                .replace(/s/i, second);
            return format;
        }
    }
    $.dateUtil = {
        /**
         * 计算时间
         * @param date [Date] 作为基准的日期对象
         * @param type [String] year, month, day
         * @param length [int] 基准时间之后:正数,基准时间之前:负数
         * @returns {*} 计算后的日期对象
         */
        calcTime: function (date, type, length) {
            // 根据类型和长度修改date对象
            var result = new Date(date.getTime());
            type == "year" ? result.setFullYear(result.getFullYear() + length)
                : type == "month" ? result.setMonth(result.getMonth() + length)
                : type == "day" ? result.setDate(result.getDate() + length)
                    : "";
            return result;
        },
        /**
         * 获得从指定时间开始,推算指定时间的日期
         * @param type [String] year, month, day
         * @param length [int] 当前时间之后:正数,当前时间之前:负数
         * @param dateStr [String] 指定的时间字符串
         * @param format [String] 生成时间的格式,具体格式见$.dateChange.formatDate
         * @returns {{current: string, result: string}}
         */
        getTimeBlock: function (type, length, dateStr, format) {
            var _self = this;
            //初始化format
            format = format ? format : "yyyy-mm-dd";
            //默认当前时间
            var date = dateStr ? new Date(dateStr) : new Date();
            var resultDate = _self.calcTime(date, type, length);
            var formatDate = $.dateChange.formatDate;
            return {
                current: formatDate(date, format),
                result: formatDate(resultDate, format)
            };
        },
        /**
         * 补足两个时间段之间的时间,返回一个数组
         * @param startTime yyyy-mm-dd
         * @param endTime yyyy-mm-dd
         * @returns {Array}
         */
        completeTwoDate: function (opt) {
            var formatDate = $.dateChange.formatDate;
            var _self = this,
                startTime = opt.startTime,
                endTime = opt.endTime,
                type = opt.type,
                length = opt.length,
                format = opt.format;
            var result = [];
            var startDate = new Date(Date.parse(startTime.replace(/-/g, "/"))),
                // var startDate = new Date(startTime),
                endDate = new Date(endTime);
            while (endDate.getTime() >= startDate.getTime()) {
                result.push(formatDate(startDate, format));
                startDate = _self.calcTime(startDate, type, length);
            }
            return result;
        }
    }
原文地址:https://www.cnblogs.com/sherryweb/p/13492080.html