JS对时间的一些操作方法

//当前时间要增加的秒数  date.AddSeconds(-1);
Date.prototype.AddSeconds = function (Seconds){
    var date = this;
    return new Date(date.getTime() + Seconds * 1000);
}
//当前时间要增加的天数  date.AddDay(-1);
Date.prototype.AddDay = function (Day) {
    //转换成秒计算
    var date = this;
    return new Date(date.getTime() + Day * 24 * 60 * 60 * 1000);
};


///转换时间格式 new Date().ToString("yyyy-MM-dd")
Date.prototype.ToString = function (format)
{
    var date = this;
    //format = format.toLowerCase();
    format = format.replace("yyyy", this.getFullYear());
    format = format.replace("MM", (this.getMonth() + 1) <= 9 ? "0" + (this.getMonth() + 1) : (this.getMonth() + 1));
    format = format.replace("dd", this.getDate() <= 9 ? "0" + this.getDate() : this.getDate());
    format = format.replace("hh", this.getHours() <= 9 ? "0" + this.getHours() : this.getHours());
    format = format.replace("mm", this.getMinutes() <= 9 ? "0" + this.getMinutes() : this.getMinutes());
    format = format.replace("ss", this.getSeconds() <= 9 ? "0" + this.getSeconds() : this.getSeconds());
    return format;
}
//// Json /Date(2367828670431)时间戳转换成正常时间
String.prototype.formatJsonDate = function (format) {
    var date = this;
    if (typeof (date) == "undefined" || date == null)
        return "";
    var dt = new Date(parseInt(this.slice(6, 19)));
    format = format.replace("yyyy", dt.getFullYear());
    format = format.replace("MM", (dt.getMonth() + 1) <= 9 ? "0" + (dt.getMonth() + 1) : (dt.getMonth() + 1));
    format = format.replace("dd", dt.getDate() <= 9 ? "0" + dt.getDate() : dt.getDate());
    format = format.replace("hh", dt.getHours() <= 9 ? "0" + dt.getHours() : dt.getHours());
    format = format.replace("mm", dt.getMinutes());
    format = format.replace("ss", dt.getSeconds());
    return format;
}
//var createTime = "2017-09-05T13:08:56.080";
//var date = new Date(createTime.replace('T', " ")).pattern("yyyy-MM-dd hh:mm:ss")
Date.prototype.pattern = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份  
        "d+": this.getDate(), //日  
        "h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, //小时  
        "H+": this.getHours(), //小时  
        "m+": this.getMinutes(), //分  
        "s+": this.getSeconds(), //秒  
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度      
        "S": this.getMilliseconds() //毫秒      
    };
    var week = {
        "0": "\u65e5",
        "1": "\u4e00",
        "2": "\u4e8c",
        "3": "\u4e09",
        "4": "\u56db",
        "5": "\u4e94",
        "6": "\u516d"
    };
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    if (/(E+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "\u661f\u671f" : "\u5468") : "") + week[this.getDay() + ""]);
    }
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        }
    }
    return fmt;
};

猜你喜欢

转载自blog.csdn.net/qq389216533/article/details/79815215