How to Add Days in Javascript Date Object
-
I want to Add Number of Days with the Date object and get the New Date. i used this method to add Days FromDate.setDate(FromDate.getDate() +12) But this method is not working, if the month is February EG: if the From Date is 20-02-2013, if i add 12 Days with this date, will get result as 01-03-2013. Is there any other method to add days and check its leap year or not
-
I want to Add Number of Days with the Date object and get the New Date. i used this method to add Days FromDate.setDate(FromDate.getDate() +12) But this method is not working, if the month is February EG: if the From Date is 20-02-2013, if i add 12 Days with this date, will get result as 01-03-2013. Is there any other method to add days and check its leap year or not
Are you sure you're looking at the correct month? In JavaScript, the
Date
object uses a zero-based month, so ifgetMonth
returns 2, the date is in March.var date = new Date(2012, 01, 20);
date.setDate(date.getDate() + 12);
alert(date); // Displays: "Sat Mar 03 2012 00:00:00 GMT+0000 (GMT Daylight Time)"
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
I want to Add Number of Days with the Date object and get the New Date. i used this method to add Days FromDate.setDate(FromDate.getDate() +12) But this method is not working, if the month is February EG: if the From Date is 20-02-2013, if i add 12 Days with this date, will get result as 01-03-2013. Is there any other method to add days and check its leap year or not
Can you give an example of your code that isn't working?? I tried the following and it all appears to be working just fine. http://jsfiddle.net/SrVvn/[^]
as if the facebook, twitter and message boards weren't enough - blogged
-
I want to Add Number of Days with the Date object and get the New Date. i used this method to add Days FromDate.setDate(FromDate.getDate() +12) But this method is not working, if the month is February EG: if the From Date is 20-02-2013, if i add 12 Days with this date, will get result as 01-03-2013. Is there any other method to add days and check its leap year or not
Hi Robymon, The example you have given is incorrect. Adding 12 days to 20-02-2013 will not give result as 01-03-2013, but will give 04-03-2013 The solutions given by "Richard Deeming" and "Dennis E White" are correct.
Thanks & Regards, Niral Soni