date.plugin.js 日期插件

//定义命名空间
var DatePlugin;
if (!DatePlugin) DatePlugin = {};

/*整理时间:2015-05-28*/

var defaultFormat = "yyyy-MM-dd";   //默认的日期格式
var totalDefaultFormat = "yyyy-MM-dd hh:mm:ss";
var pointFormat = "yyyy.MM.dd";    
var slashFormat = "MM/dd/yyyy";
var textFormat = "yyyy年MM月dd日";
var totalTextFormat = "yyyy年MM月dd日hh小时mm分ss秒";
var noFormat = "yyyyMMdd";

/* 转换时间格式 */
Date.prototype.Format = function (DateFormat) {
    var o = {
        "M+": this.getMonth() + 1, //month 
        "d+": this.getDate(), //day 
        "h+": this.getHours(), //hour 
        "m+": this.getMinutes(), //minute 
        "s+": this.getSeconds(), //second 
        "q+": Math.floor((this.getMonth() + 3) / 3), //quarter 
        "S": this.getMilliseconds() //millisecond 
    }
    var format = (DateFormat) ? DateFormat : defaultFormat;
    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }

    for (var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
}

/* 增加天数 */
Date.prototype.addDay = function (num) {
    this.setDate(this.getDate() + num);
    return this;
};

/* 增加月数 */
Date.prototype.addMonth = function (num) {
    var tempDate = this.getDate();
    this.setMonth(this.getMonth() + num);
    if (tempDate != this.getDate()) this.setDate(0);
    return this;
};

/* 增加年数 */
Date.prototype.addYear = function (num) {
    var tempDate = this.getDate();
    this.setYear(this.getYear() + num);
    if (tempDate != this.getDate()) this.setDate(0);
    return this;
};


/*
*获取某日日期  可传日期格式参数
*默认格式:YYYY-MM-DD
*/
DatePlugin.exactDate = {

    /* 获取今日日期 */
    getTodayDate:function (DateFormat) {
        var myDate = new Date();
        return myDate.Format(DateFormat);
    },

    /* 获取明日日期 */
    getTomorrowDate: function (DateFormat) {
        var myDate = new Date().addDay(1);
        return myDate.Format(DateFormat);
    },

    /* 获取昨天日期 */
    getYesterdayDate: function (DateFormat) {
        var myDate = new Date().addDay(-1);
        return myDate.Format(DateFormat);
    }
}

/*
*统计某个时间段的周数或天数或者返回值是数字
*/
DatePlugin.staticDate = {

    /*统计今年某月的天数*/
    getMonthDays: function (Month) {
        var myMonth = Month;
        var now = new Date();                    //当前日期      
        var nowYear = now.getFullYear();         //当前年     
        var monthStartDate = new Date(nowYear, myMonth, 1);
        var monthEndDate = new Date(nowYear, myMonth + 1, 1);
        var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
        return days;
    },

    /*统计某年某月总周数*/
    getWeekCounts: function (year, month) {
        var firstDate = new Date(year, month, 1);
        var first = firstDate.getDay();
        var endDate = 6;     // 周六
        var firstWeekday = endDate - first + 1;
        var lastDay = this.getMonthDays(month) - firstWeekday;
        return Math.ceil(lastDay / 7) + 1;
    },

    /*获取当前月*/
    getCurrentMonth:function () {
        var now = new Date(); //当前日期 
        var nowDayOfWeek = now.getDay(); //今天本周的第几天 
        var nowDay = now.getDate(); //当前日 
        var nowMonth = now.getMonth() + 1; //当前月 
        var nowYear = now.getFullYear(); //当前年 
        return nowMonth;
    }
}

DatePlugin.calculDate = {

    /*计算几天前或几天后的日期*/
    getTrunsAddDay: function (date,num,DateFormat) {
        var targetday_milliseconds = date.getTime() + 1000 * 60 * 60 * 24 * num;
        var resultDate = new Date(date.setTime(targetday_milliseconds));
        return resultDate.Format(DateFormat);
    },

    /*获得本月的开始日期*/
    getMonthStartDate: function (DateFormat) {
        var now = new Date();                    //当前日期     
        var nowMonth = now.getMonth();           //当前月     
        var nowYear = now.getFullYear();             //当前年     
        var monthStartDate = new Date(nowYear, nowMonth, 1);
        return monthStartDate.Format(DateFormat);
    },

    /*获得本月的结束日期*/
    getMonthEndDate: function (DateFormat) {
        var now = new Date();                    //当前日期     
        var nowDayOfWeek = now.getDay();         //今天本周的第几天     
        var nowDay = now.getDate();              //当前日     
        var nowMonth = now.getMonth();           //当前月     
        var nowYear = now.getFullYear();             //当前年     
        var monthEndDate = new Date(nowYear, nowMonth, DatePlugin.staticDate.getMonthDays(nowMonth));
        return monthEndDate.Format(DateFormat);
    },

    /*获得某个月的开始日期*/
    getMonthStart: function (currDate, DateFormat) {
        var now = new Date(currDate);          //当前日期  
        var nowMonth = parseInt(now.getMonth());        //当前月     
        var nowYear = now.getFullYear();         //当前年     

        var monthStartDate = new Date(nowYear, nowMonth, 1);
        return monthStartDate.Format(DateFormat);
    },

    /*获得某个月的结束日期*/
    getMonthEnd: function (currDate, DateFormat) {
        var now = new Date(currDate);                    //当前日期
        var nowMonth = parseInt(now.getMonth());        //当前月     
        var nowYear = now.getFullYear();         //当前年     

        var monthEndDate = new Date(nowYear, nowMonth, DatePlugin.staticDate.getMonthDays(nowMonth));
        return monthEndDate.Format(DateFormat);
    },

    /*获得当天开始前一个月的开始日期*/
    getBeforeMonth: function (DateFormat) {
        var now = new Date();                    //当前日期     
        var nowMonth = now.getMonth() - 1;           //当前月     
        var nowYear = now.getFullYear();             //当前年     
        var monthDate;
        if (nowMonth != -1) {
            monthDate = new Date(nowYear, nowMonth);
        } else {
            monthDate = new Date(nowYear - 1, 11);
        }
        return monthDate.Format(DateFormat);
    },

    /*获取最近一周开始时间*/
    getBeforeOneWeekStartDate: function (DateFormat) {
        var nowdate = new Date();
        return oneweekkdate = this.getTrunsAddDay(nowdate, -6, DateFormat);
    },

    /*获取三个月以内的起始时间*/
    getBeforeThreeMonthStartDate: function (DateFormat) {
        var nowdate = new Date();
        return threemonthdate = this.getTrunsAddDay(nowdate, -90, DateFormat);
    },

    /*获取一个月以内的起始时间*/
    getBeforeOneMonthStratDate: function (DateFormat) {
        var nowdate = new Date();
        return threemonthdate = this.getTrunsAddDay(nowdate, -29, DateFormat);
    }

}

DatePlugin.otherDate = {

    /*ISO8601*/
    parseISO8601:function(dateStringInRange) {
        var isoExp = /^s*(d{4})-(dd)-(dd)s*$/,
            date = new Date(NaN), month,
            parts = isoExp.exec(dateStringInRange);

        if (parts) {
            month = +parts[2];
            date.setFullYear(parts[1], month - 1, parts[3]);
            if (month != date.getMonth() + 1) {
                date.setTime(NaN);
            }
        }
        return date;
    },

    /*处理日期格式兼容浏览器方法*/
    newDate:function(str) {
        str = str.split('-');
        var date = new Date();
        date.setUTCFullYear(str[0], str[1] - 1, str[2]);
        date.setUTCHours(0, 0, 0, 0);
        return date;
    }
}
 
原文地址:https://www.cnblogs.com/wanliyuan/p/4597481.html