Coder Perfect

How do I multiply today’s date by the number of days? [duplicate]

Problem

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

Asked by Linda725

Solution #1

You can use JavaScript without jQuery:

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

Answered by p.campbell

Solution #2

This is for a 5-day period:

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

You don’t need JQuery to do this; you can do it in JavaScript.

Answered by user3945851

Solution #3

This is how you could expand the javascript Date object.

Date.prototype.addDays = function(days) {
    this.setDate(this.getDate() + parseInt(days));
    return this;
};

and you could call it from your javascript code

var currentDate = new Date();
// to add 4 days to current date
currentDate.addDays(4);

Answered by Krishna Chytanya

Solution #4

Why not just utilize it?

function addDays(theDate, days) {
    return new Date(theDate.getTime() + days*24*60*60*1000);
}

var newDate = addDays(new Date(), 5);

or -5 to get rid of 5 days

Answered by user3138856

Solution #5

Moment.js

Now is the time to install. From here, you can get JavaScript.

npm : $ npm i –save moment

install bower —save-moment $ 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

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

Moment.js is a fantastic Javascript framework for managing Date objects that is only 40kb in size.

Good Luck.

Answered by Aakash

Post is based on https://stackoverflow.com/questions/3818193/how-to-add-number-of-days-to-todays-date