JS 日期封装操作

1 篇文章 0 订阅
1 篇文章 0 订阅

 JS对日期封装使用,采用正则匹配日期结构进行显示日期。

/**
 * 字符串转化为日期对象======》 调用格式为:str.format2Date("yyyy-MM-dd hh:mm:ss")
 * @param {str} 传入特殊格式
 * @return {Date}
 * */
String.prototype.format2Date = function(style, option) {
    if (style) {
        let y = style.match("[Yy]{2,4}");
        let M = style.match("M{1,2}");
        let d = style.match("d{1,2}");
        let h = style.match("h{1,2}");
        let m = style.match("m{1,2}");
        let s = style.match("s{1,2}");
        let year = this.substring(y.index, y.index+y[0].length);
        let month = this.substring(M.index, M.index+M[0].length);
        let day = this.substring(d.index, d.index+d[0].length);
        let hour = h != null?this.substring(h.index, h.index+h[0].length):'00';
        let minute = m != null?this.substring(m.index, m.index+m[0].length):'00';
        let second = s != null?this.substring(s.index, s.index+s[0].length):'00';

        return new Date(year+"/"+month+"/"+day+"  "+hour+":"+minute+":"+second)
    } else {
        return new Date(this);
    }
};


/**
 * 时间日期格式化======》 调用格式为:date.format2String("yyyy-MM-dd hh:mm:ss")
 * @param {[type]} style 时间格式字符串
 * @returns {String}
 */
Date.prototype.format2String = function(style, option) {
    var o = {
        "[Yy]{2,4}" : this.getFullYear(),   //year
        "M{1,2}" : this.getMonth() + 1,     //month
        "d{1,2}" : this.getDate(),          //day
        "H{1,2}" : this.getHours(),         //hour
        "h{1,2}" : this.getHours()%24,      //hour
        "m{1,2}" : this.getMinutes(),       //minute
        "s{1,2}" : this.getSeconds(),       //second
        "[Ee]" : this.getDay(),             //day in week
        "[Ww]" : this.getDay(),             //day in week
        "[Qq]" : Math.floor((this.getMonth()+3)/3),  //quarter
        "S{3}|S{1}" : this.getMilliseconds()//millisecond
    };
    for(var k in o ){
        style = style.replace(new RegExp("("+ k +")"), function(match, g, offset, string){
            return ("0".repeat(match.length)+o[k]).substr( (""+o[k]).length );
        })
    }
    return style;
};

/**
 * 日期相减函数
 * @param {date } 计算相减日期(传入日期无先后顺序)
 *
 * */
Date.prototype.dateSubtraction = function(date = new Date()) {
    var start = Date.parse(new Date(this.replace(/-|\/|\,/g, "\/")));
    var end = Date.parse(new Date(date.replace(/-|\/|\,/g, "\/")));
    if (start > end) {
        return parseInt((this.getTime() - date.getTime()) / (1000 * 60 * 60 * 24));
    } else {
        return parseInt((date.getTime() - this.getTime()) / (1000 * 60 * 60 * 24));
    }
};

/**
 * 年:加/减
 * @param type 计算类型(1:相加,2:相减)
 * @param {day} 相加或者相减天数
 *
 * */
Date.prototype.yearCompute = function(type, day = 1) {
    var date = new Date(this);
    switch (type) {
        case 1: {
            date.setFullYear(parseInt(this.getFullYear() + day));console.log("12313", date);
            return date;
        }
        case 2: {
            date.setFullYear(parseInt(this.getFullYear() - day));
            return date;
        }
    }
};

/**
 * 月:加/减
 * @param type 计算类型(1:相加,2:相减)
 * @param {month} 相加或者相减天数
 *
 * */
Date.prototype.monthCompute = function(type, month = 1) {
    var date = new Date(this);
    switch (type) {
        case 1: {
            date.setMonth(parseInt(this.getMonth() + month));
            return date;
        }
        case 2: {
            date.setMonth(parseInt(this.getMonth() - month));
            return date;
        }
    }
};

/**
 * 天:加/减
 * @param type 计算类型(1:相加,2:相减)
 * @param {day} 相加或者相减天数
 *
 * */
Date.prototype.dayCompute = function(type, day = 1) {
    var date = new Date(this);
    switch (type) {
        case 1: {
            date.setDate(parseInt(this.getDate() + day));
            return date;
        }
        case 2: {
            date.setDate(parseInt(this.getDate() - day));
            return date;
        }
    }
};

/**
 * 小时:加/减
 * @param type 计算类型(1:相加,2:相减)
 * @param {hour} 相加或者相减小时
 *
 * */
Date.prototype.hourCompute = function(type, hour = 1) {
    var date = new Date(this);
    switch (type) {
        case 1: {
            date.setHours(parseInt(this.getHours() + hour));
            return date;
        }
        case 2: {
            date.setHours(this.getHours() - hour);
            return date;
        }
    }
};


/**
 * 分钟:加/减
 * @param type 计算类型(1:相加,2:相减)
 * @param {minute} 相加或者相减小时
 *
 * */
Date.prototype.minuteCompute = function(type, minute = 1) {
    var date = new Date(this);
    switch (type) {
        case 1: {
            date.setMinutes(parseInt(this.getMinutes() + minute));
            return date;
        }
        case 2: {
            date.setMinutes(this.getMinutes() - minute);
            return date;
        }
    }
};

/**
 * 秒钟:加/减
 * @param type 计算类型(1:相加,2:相减)
 * @param {second} 相加或者相减小时
 *
 * */
Date.prototype.secondCompute = function(type, second = 1) {
    var date = new Date(this);
    switch (type) {
        case 1: {
            date.setSeconds(parseInt(this.getSeconds() + second));
            return date;
        }
        case 2: {
            date.setSeconds(parseInt(this.getSeconds() - second));
            return date;
        }
    }
};


/**
 * 毫秒:加/减
 * @param type 计算类型(1:相加,2:相减)
 * @param {millisecond} 相加或者相减小时
 *
 * */
Date.prototype.millisecondCompute = function(type, millisecond = 1) {
    var date = new Date(this);
    switch (type) {
        case 1: {
            date.setMilliseconds(parseInt(this.getMilliseconds() + millisecond));
            return date;
        }
        case 2: {
            date.setMilliseconds(parseInt(this.getMilliseconds() - millisecond));
            return date;
        }
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值