How to add number of days to today's date? [du

2018-12-31 03:30发布

This question already has an answer here:

I need to be able to add 1, 2 , 5 or 10 days to today's date using jQuery.

16条回答
流年柔荑漫光年
2楼-- · 2018-12-31 04:07

Moment.js

Install moment.js from here.

npm : $ npm i --save moment

Bower : $ bower install --save moment

Next,

var date = moment()
            .add(2,'d') //replace 2 with number of days you want to add
            .toDate(); //convert it to a Javascript Date Object if you like

Link Ref : http://momentjs.com/docs/#/manipulating/add/

Moment.js is an amazing Javascript library to manage Date objects and extremely light weight at 40kb.

Good Luck.

查看更多
其实,你不懂
3楼-- · 2018-12-31 04:07

function addDays(n){
    var t = new Date();
    t.setDate(t.getDate() + n); 
    var month = "0"+(t.getMonth()+1);
    var date = "0"+t.getDate();
    month = month.slice(-2);
    date = date.slice(-2);
     var date = date +"/"+month +"/"+t.getFullYear();
    alert(date);
}

addDays(5);

查看更多
只若初见
4楼-- · 2018-12-31 04:11

I found that JavaScript can return a correct date when you use new Date(nYear, nMonth, nDate); with the over days of that month. Try to see the result of a dDate variable when you use this:

var dDate = new Date(2012, 0, 34); // the result is 3 Feb 2012


I have a SkipDate function to share:

    function DaysOfMonth(nYear, nMonth) {
        switch (nMonth) {
            case 0:     // January
                return 31; break;
            case 1:     // February
                if ((nYear % 4) == 0) {
                    return 29;
                }
                else {
                    return 28;
                };
                break;
            case 2:     // March
                return 31; break;
            case 3:     // April
                return 30; break;
            case 4:     // May
                return 31; break;
            case 5:     // June
                return 30; break;
            case 6:     // July
                return 31; break;
            case 7:     // August
                return 31; break;
            case 8:     // September
                return 30; break;
            case 9:     // October
                return 31; break;
            case 10:     // November
                return 30; break;
            case 11:     // December
                return 31; break;
        }
    };

    function SkipDate(dDate, skipDays) {
        var nYear = dDate.getFullYear();
        var nMonth = dDate.getMonth();
        var nDate = dDate.getDate();
        var remainDays = skipDays;
        var dRunDate = dDate;

        while (remainDays > 0) {
            remainDays_month = DaysOfMonth(nYear, nMonth) - nDate;
            if (remainDays > remainDays_month) {
                remainDays = remainDays - remainDays_month - 1;
                nDate = 1;
                if (nMonth < 11) { nMonth = nMonth + 1; }
                else {
                    nMonth = 0;
                    nYear = nYear + 1;
                };
            }
            else {
                nDate = nDate + remainDays;
                remainDays = 0;
            };
            dRunDate = Date(nYear, nMonth, nDate);
        }
        return new Date(nYear, nMonth, nDate);
    };
查看更多
倾城一夜雪
5楼-- · 2018-12-31 04:15

This is for 5 days:

var myDate = new Date(new Date().getTime()+(5*24*60*60*1000));

You don't need JQuery, you can do it in JavaScript, Hope you get it.

查看更多
无与为乐者.
6楼-- · 2018-12-31 04:15

The accepted answer here gave me unpredictable results, sometimes weirdly adding months and years.

The most reliable way I could find was found here Add days to Javascript Date object, and also increment month

var dayOffset = 20;
var millisecondOffset = dayOffset * 24 * 60 * 60 * 1000;
december.setTime(december.getTime() + millisecondOffset); 

EDIT: Even though it has worked for some people, I don't think it is entirely correct. I would recommend going with a more popular answer or using something like http://momentjs.com/


查看更多
有味是清欢
7楼-- · 2018-12-31 04:16

You can use JavaScript, no jQuery required:

var someDate = new Date();
var numberOfDaysToAdd = 6;
someDate.setDate(someDate.getDate() + numberOfDaysToAdd); 

Formatting to dd/mm/yyyy :

var dd = someDate.getDate();
var mm = someDate.getMonth() + 1;
var y = someDate.getFullYear();

var someFormattedDate = dd + '/'+ mm + '/'+ y;
查看更多
登录 后发表回答