简体   繁体   中英

how to calculate the 5th date from a date in javascript

I have a date in a javascript variable

var cDate='07/21/2012'  `(mm/dd/YYY format)`

I need to find out the 5th day from the date in cDate Variable

ie newDate= '07/26/2012'

suppose if the cDate='07/28/2012' then newDate='08/2/2012' But I did know how. I have tried a lot and searched but my result is wrong.

Please replay

thanks in advance

myDate = new Date(2012, 07, 28)
new Date(myDate.getTime() + 5*24*60*60*1000);

OR extend the Date Class as

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

Or you can even use this library. http://www.datejs.com/

Have you tried the following?

cDate = new Date(2012, 07, 28)
cDate.setDate(cDate.getDate() + 5);

please try this

var myDate=new Date();
myDate.setDate(myDate.getDate()+5);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM