关闭

javascript日期加减——年、月、日

226人阅读 评论(0) 收藏 举报
分类:

javascript日期加减——年、月、日

javascript的日期加减,能够进行日期的加减(月份只能加,不能减),下面是代码和截图:
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script src="test.js"></script>
</head>
<body>
    <h1>hello world</h1>
</body>
</html>

/**
 * Created by Administrator on 2016-08-31.
 */
window.onload = function () {
    var nowdate = new Date();
    console.log(dateToString(nowdate));
    console.log(dateToString(dateAddDate(nowdate, 2)));
    console.log(dateToString(dateAddMonth(nowdate,12)));

    var testdate = '2016-8-30';
    var temptestdate = dateAddMonth(testdate, -6);
    console.log(dateToString(temptestdate));
    console.log(dateToString(dateAddYear(temptestdate, 1)));

};

/**
 * 日期转换成 2016-8-31形式
 * @param date
 * @returns {string}
 */
var dateToString = function (date) {
    return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
};

/**
 * 日期加上任意天数或减去任意天数
 * @param date
 * @param dayCount
 * @returns {Date}
 */
var dateAddDate = function (date, dayCount) {
    var tempDate = dateToDate(date);
    var count = parseInt(dayCount);
    var oldYear = tempDate.getFullYear();
    var oldMonth = tempDate.getMonth();
    var oldDate = tempDate.getDate();
    var newDate = oldDate + count;
    return new Date(oldYear, oldMonth, newDate);
};

/**
 * 把字符串转换成Date,如果是Date类型,则不转换。2016-8-31
 * @param date
 * @returns {Date}
 */
function dateToDate(date) {
    var sDate = new Date();
    if (typeof date == 'object' && typeof new Date().getMonth == "function") {
        sDate = date;
    } else if (typeof date == "string") {
        var arr = date.split('-');
        if (arr.length == 3) {
            sDate = new Date(arr[0] + '-' + arr[1] + '-' + arr[2]);
        }
    }
    return sDate;
};

/**
 * 日期加上任意月份
 * @param date
 * @param monthCount
 * @returns {Date}
 */
var dateAddMonth = function (date, monthCount) {
    var tempDate = dateToDate(date);
    var count = parseInt(monthCount);
    if(count < 0){
        return new Date();
    }
    var oldYear = tempDate.getFullYear();
    var oldMonth = tempDate.getMonth();
    var oldDate = tempDate.getDate();
    var newMonth = oldMonth + count;
    var newDate = new Date(oldYear, newMonth, oldDate);
    //防止月份数不一致,进行微调
    while (newDate.getMonth() != (newMonth % 12)) {
        oldDate--;
        newDate = new Date(oldYear, newMonth, oldDate);
    }
    return newDate;
};

/**
 * 日期加减年
 * @param date
 * @param yearCount
 * @returns {Date}
 */
var dateAddYear = function (date, yearCount) {
    var tempDate = dateToDate(date);
    var count = parseInt(yearCount);
    var oldYear = tempDate.getFullYear();
    var oldMonth = tempDate.getMonth();
    var oldDate = tempDate.getDate();
    var newYear = oldYear + count;
    var newDate = new Date(newYear, oldMonth, oldDate);
    //防止月份数不一致,进行微调
    while (newDate.getMonth() != oldMonth) {
        oldDate--;
        newDate = new Date(newYear, oldMonth, oldDate);
    }
    return newDate;
};

运行结果:

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:353331次
    • 积分:7954
    • 等级:
    • 排名:第2756名
    • 原创:442篇
    • 转载:12篇
    • 译文:7篇
    • 评论:39条
    文章分类
    最新评论