13
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

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
250091017
  • 141
  • 1
  • 1
  • 7

4 Answers4

14

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

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
7

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,
  ...
);
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
6

try this

var date = new Date();
var numberToAdd = 1;
date.setDate(date.getDate() + numberToAdd); 
Nikson Kanti Paul
  • 3,394
  • 1
  • 35
  • 51
0

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;
        };
Jacobi
  • 1,508
  • 15
  • 29