250091017
250091017

Reputation: 141

How to add days to the now date in javascript?

var myDate = new Date();
var endtime= new Date(myDate.getDate()+1,23:59:59);
alert(endtime);

why there is no value of the endtime? if i want to add 1 day 10 hours 50 minute 30 second to the now time, how to wirte the endtime code? thank you

Upvotes: 13

Views: 26642

Answers (4)

Jacobi
Jacobi

Reputation: 1524

I always create 7 functions, to work with date in JS: addSeconds, addMinutes, addHours, addDays, addWeeks, addMonths, addYears.

You can see an example here: http://jsfiddle.net/tiagoajacobi/YHA8x/

How to use:

var now = new Date();
console.log(now.addWeeks(3));

This are the functions:

        Date.prototype.addSeconds = function(seconds) {
            this.setSeconds(this.getSeconds() + seconds);
            return this;
        };

        Date.prototype.addMinutes = function(minutes) {
            this.setMinutes(this.getMinutes() + minutes);
            return this;
        };

        Date.prototype.addHours = function(hours) {
            this.setHours(this.getHours() + hours);
            return this;
        };

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

        Date.prototype.addWeeks = function(weeks) {
            this.addDays(weeks*7);
            return this;
        };

        Date.prototype.addMonths = function (months) {
            var dt = this.getDate();

            this.setMonth(this.getMonth() + months);
            var currDt = this.getDate();

            if (dt !== currDt) {  
                this.addDays(-currDt);
            }

            return this;
        };

        Date.prototype.addYears = function(years) {
            var dt = this.getDate();

            this.setFullYear(this.getFullYear() + years);

            var currDt = this.getDate();

            if (dt !== currDt) {  
                this.addDays(-currDt);
            }

            return this;
        };

Upvotes: 0

Bergi
Bergi

Reputation: 665485

You will need to add the days in milliseconds:

var tomorrow = new Date(Date.now() + 1 * 24*3600*1000);

Of course you can add various amounts of time, you just need to count it in milliseconds when using the Date constructor or set/getTime().

You can also set the different units one-by-one using their respective Date methods:

var sometime = new Date; // now
sometime.setDate(sometime.getDate() + numberOfDays); 
sometime.setHours(sometime.getHours() + numberOfHours); 
sometime.setMinutes(sometime.getMinutes() + numberOfMinutes);
...

You can't set the Date with a float value, it will be truncated when beeing converted to an Integer.

But the setter methods higher than milliseconds and higher than date have optional attributes, so that you can combine the setting:

var sometime = new Date; // now
sometime.setFullYear(
  sometime.getFullYear() + numberOfYears,
  sometime.getMonth() + numberOfMonths,
  sometime.getDate() + numberOfDays
); 
sometime.setHours(
  sometime.getHours() + numberOfHours,
  sometime.getMinutes() + numberOfMinutes,
  ...
);

Upvotes: 7

Pranay Rana
Pranay Rana

Reputation: 176956

Try one of the two way will work for you...

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

DEMO1

or

var today = new Date();
var tomorrow = new Date();
tomorrow.setDate(today.getDate()+1); 

DEMO2

Upvotes: 14

Nikson Kanti Paul
Nikson Kanti Paul

Reputation: 3440

try this

var date = new Date();
var numberToAdd = 1;
date.setDate(date.getDate() + numberToAdd); 

Upvotes: 6

Related Questions