JS日期加减

//获取当前日期
var curDate = function() {
        var cur_date = new Date();
        var thanksTheYear = cur_date.getFullYear();
        var thanksTheMonth = cur_date.getMonth() + 1;
        if (thanksTheMonth < 10) {
            thanksTheMonth = "0" + thanksTheMonth;
            var today = cur_date.getDate();
            if (today < 10) {
                today = "0" + today;
            }
            var curDateStr = thanksTheYear + "-" + thanksTheMonth + "-" + today;
            return curDatestr;
        };
    }
    //日期加减
var addDate = function(date, day) {
    var time = new Date(date);
    time.setDate(time.getDate() + day);
    var theYear = time.getFullYear();
    var theMonth = time.getMonth() + 1;
    if (theMonth < 10) {
        theMonth = "0" + theMonth;
    }
    var theToday = time.getDate();
    if (theToday < 10) {
        theToday = "0" + theToday;
    }
    var timeStr = theYear + "-" + theMonth + "-" + theToday;
    return timeStr;
};

//日期加减(月)
var addDate = function(date, month) {
    var time = new Date(date);
    time.setMonth(time.getMonth() + month);
    var theYear = time.getFullYear();
    var theMonth = time.getMonth() + 1;
    if (theMonth < 10) {
        theMonth = "0" + theMonth;
    }
    var theToday = time.getDate();
    if (theToday < 10) {
        theToday = "0" + theToday;
    }
    var timeStr = theYear + "-" + theMonth + "-" + theToday;
    return timeStr;
};
//日期加减(天)
var addDateDay = function(date, day) {
    var time = new Date(date);
    time.setDate(time.getDate() + day);
    var theYear = time.getFullYear();
    var theMonth = time.getMonth() + 1;
    if (theMonth < 10) {
        theMonth = "0" + theMonth;
    }
    var theToday = time.getDate();
    if (theToday < 10) {
        theToday = "0" + theToday;
    }
    var timeStr = theYear + "-" + theMonth + "-" + theToday;
    return timestr;
};
//日期加减(年)
var addDateYear = function(date, year) {
    var time = new Date(date);
    time.setFullYear(time.getFullYear() + year);
    var theYear = time.getFullYear();
    var theMonth = time.getMonth() + 1;
    if (theMonth < 10) {
        theMonth = "0" + theMonth;
    }
    var theToday = time.getDate();
    if (theToday < 10) {
        theToday = "0" + theToday;
    }
    var timeStr = theYear + "-" + theMonth + "-" + theToday;
    return timeStr;
};

你可能感兴趣的:(原创,javascript)