Suppose I get a date via a jquery calendar to a java script variable. e.g: var d = 02/12/2011
How can I manipulate that date variable using a js function or jq method where to get the date 3 month ahead of that date? I cant just do following know. Because every month does not have 30 days?
var futureDate=n开发者_StackOverflow中文版ew Date(d);
futureDate.setDate(futureDate.getDate()+30*3);
You can try using either the Date.js or Sugar.js libraries. They both have great date manipulation functions.
Here's an example using Sugar...
var futureDate = Date.create(d);
futureDate.addMonths(3);
The value d can be anything that Sugar understands as a date which is quite flexible.
Use futureDate.setMonth(futureDate.getMonth() + 3)
This will work towards the end of the year too. It roll over to the new year automatically.
Here's a good routine that can handle that and lots of other date manipulations:
http://slingfive.com/pages/code/jsDate/jsDate.html
精彩评论