JavaScript Date类型扩展

日期转换成指定格式字符串

/**
 * 把日期格式化成指定字符串
 * y|yyyy|yy:年 
 * M|MM:月
 * d|dd:日
 * H|HH:小时
 * m|mm:分钟
 * s|ss:秒
 * f:毫秒
 * @param {string} patten 格式字符串,如 yyyy-MM-dd HH:mm:ss
 * @returns 返回格式化后的字符串
 */
Date.prototype.Format = function (patten) {
    if (/(yyyy)/g.test(patten)) {
        patten = patten.replace(RegExp.$1, this.getFullYear());
    }
    else if (/(yy)/g.test(patten)) {
        patten = patten.replace(RegExp.$1, this.getFullYear().toString().substr(2, 2));
    }
    else if (/(y)/g.test(patten)) {
        patten = patten.replace(RegExp.$1, this.getFullYear());
    }

    if (/(MM)/g.test(patten)) {
        patten = patten.replace(RegExp.$1, ('0' + (this.getMonth() + 1)).slice(-1 * 2));
    }
    else if (/(M)/g.test(patten)) {
        patten = patten.replace(RegExp.$1, (this.getMonth() + 1));
    }

    if (/(dd)/g.test(patten)) {
        patten = patten.replace(RegExp.$1, ('0' + this.getDate()).slice(-1 * 2));
    }
    else if (/(d)/g.test(patten)) {
        patten = patten.replace(RegExp.$1, this.getDate());
    }

    if (/(HH)/g.test(patten)) {
        patten = patten.replace(RegExp.$1, ('0' + this.getHours()).slice(-1 * 2));
    }
    else if (/(H)/g.test(patten)) {
        patten = patten.replace(RegExp.$1, this.getHours());
    }

    if (/(mm)/g.test(patten)) {
        patten = patten.replace(RegExp.$1, ('0' + this.getMinutes()).slice(-1 * 2));
    }
    else if (/(m)/g.test(patten)) {
        patten = patten.replace(RegExp.$1, this.getMinutes());
    }

    if (/(ss)/g.test(patten)) {
        patten = patten.replace(RegExp.$1, ('0' + this.getSeconds()).slice(-1 * 2));
    }
    else if (/(s)/g.test(patten)) {
        patten = patten.replace(RegExp.$1, this.getSeconds());
    }

    if (/(f+)/g.test(patten)) {
        patten = patten.replace(RegExp.$1, this.getMilliseconds());
    }

    return patten;
}

判断日期是否为闰年

/**
 * 判断是否为闰年
 * @returns true/false
 */
Date.prototype.IsLeapyearr = function () {
    let year = this.getFullYear();
    return (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0);
}

日期增加及减少

/**
 * 在指定的日期上增加指定的年数
 * @param {number} increase 增长值,整数
 * @returns 增加后的日期
 */
Date.prototype.AddYears = function (increase) {
    let year = this.getFullYear();
    let oldDate = this.Format('yyyy-MM-dd HH:mm:ss')
    let newDate = oldDate.replace(year.toString(), (year + increase).toString());
    return new Date(newDate);
}

/**
 * 在指定的日期上增加指定的月数
 * @param {number} increase 增长值,整数
 * @returns 增加后的日期
 */
Date.prototype.AddMonths = function (increase) {
    let year = this.getFullYear();
    let month = this.getMonth() + 1 + increase;
    let day = this.getDate();
    if (month > 12) {
        let increaseYear = Math.floor(month / 12);
        year += increaseYear;
        month = month % 12;
    }
    let bigMonth = [1, 3, 5, 7, 8, 10, 11, 12];
    if (bigMonth.indexOf(month) == -1 && day > 28) {
        if (month == 2) {
            //判断是否为闰年
            let leapYear = (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0);
            day = leapYear ? 29 : 28;
        }
        else {
            day = day == 31 ? 30 : day;
        }
    }
    let hour = this.getHours();
    let minute = this.getMinutes();
    let second = this.getSeconds();
    let date = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
    return new Date(date);
}

/**
 * 在指定的日期上增加指定的天数
 * @param {number} increase 增长值,整数
 * @returns 增加后的日期
 */
Date.prototype.AddDays = function (increase) {
    let time = this.getTime();
    return new Date(time + increase * 86400000);
}

/**
 * 在指定的日期上增加指定的小时数
 * @param {number} increase 增长值,整数
 * @returns 增加后的日期
 */
Date.prototype.AddHours = function (increase) {
    let time = this.getTime();
    return new Date(time + increase * 3600000)
}

/**
 * 在指定的日期上增加指定的分钟数
 * @param {number} increase 增长值,整数
 * @returns 增加后的日期
 */
Date.prototype.AddMinutes = function (increase) {
    let time = this.getTime();
    return new Date(time + increase * 60000)
}

/**
 * 在指定的日期上增加指定的秒数
 * @param {number} increase 增长值,整数
 * @returns 增加后的日期
 */
Date.prototype.AddSeconds = function (increase) {
    let time = this.getTime();
    return new Date(time + increase * 1000)
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值