views:

80

answers:

4

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

I'm a novice and trying hard to learn.

+5  A: 

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;
p.campbell
Like most things javascript, the built-in date processing is extremely powerful, but completely non-intuitive.
Joel Coehoorn
@Joel: agreed, it's a headshaker that `setDate()`. Non-intuitive is a good descriptor.
p.campbell
[Date object](https://developer.mozilla.org/en/JavaScript/Reference/global_objects/date) for reference.
Reigel
@Joel: that problem is not exclusive to JavaScript. Date/Time API are traditionally a bit weird.
Thilo
how do i get it in dd/mm/yyyy format?
Linda725
@Linda: updated for formatting as you like.
p.campbell
+1  A: 

You could extend the javascript Date object like this

Date.prototype.addDays = function(days) {
    var newDate = new Date((this.valueOf() + (days * 86400000)));
    return newDate;
};

and in your javascript code you could call

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

You can use this library "Datejs open-source JavaScript Date Library".

andres descalzo
A: 

A suggestion: DON'T use milliseconds, or maybe daylight-saving-time will occur.

iwill