时间插件,js格式化,js某月天数,js某月最后一天日期

//时间格式化

Date.prototype.format = function(fmt) {  
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "h+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if(/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    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;
}

  

//得到某天的前天,后天

Date.prototype.getCountDate = function(num) {
    return new Date(this.setDate(this.getDate()+num)).format('yyyy-MM-dd');
}

  

//某月最后一天

Date.prototype.getMonthEnd = function() {
    return new Date(this.getFullYear(),this.getMonth()+1).toJSON().substring(0,10);
}

  

//某月多少天

Date.prototype.getMonthEnd = function() {
    var curMonth = this.getMonth();
    this.setMonth(curMonth + 1);
    this.setDate(0);
    return this.getDate();
}

  

//某日是某年的第几周

Date.prototype.getTheWeek = function() {
    var totalDays = 0, now=this;
    var years = now.getFullYear()
    if (years < 1000)
        years += 1900
    var days = new Array(12);
    days[0] = 31;
    days[2] = 31;
    days[3] = 30;
    days[4] = 31;
    days[5] = 30;
    days[6] = 31;
    days[7] = 31;
    days[8] = 30;
    days[9] = 31;
    days[10] = 30;
    days[11] = 31;

    //判断是否为闰年,针对2月的天数进行计算
    if (Math.round(now.getYear() / 4) == now.getYear() / 4) {
        days[1] = 29
    } else {
        days[1] = 28
    }

    if (now.getMonth() == 0) {
        totalDays = totalDays + now.getDate();
    } else {
        var curMonth = now.getMonth();
        for (var count = 1; count <= curMonth; count++) {
            totalDays = totalDays + days[count - 1];
        }
        totalDays = totalDays + now.getDate();
    }
    //得到第几周
    var week = Math.ceil(totalDays / 7);
    return week;
}

  

原文地址:https://www.cnblogs.com/jiebba/p/7270177.html