JavaScript Hacks: Add days to a date
If you were googling for a method to add a few days to a given date in JavaScript, you may be running up a blind alley.
As I painfully learnt that there is a simple way to accomplish this. If you are asking what’s so complex about that, here’s how: Subtract 10 days from the current date. Simply put, find today’s date (day of month as in Date().getDate()), subtract 10 from it. If that is less than 0, wrap around the date to the previous month after finding out the number of days in the previous month. Now if the month was January, wrap around to the previous year. etc.. Simple!!
Now that’s a tad bit too much of coding logic for something as simple as that. There has got to be a much simpler way. Isn’t there a dateAdd or dateSub kinda function? Oops.. Sorry. Gotta write that yourself.
But here’s the catch. JavaScript is foolish enough if we know how to outsmart it with its own Date() constructor.
The Date() object has a constructor that accepts parameters in the following format:
var someDate = new Date(iYear, iMonth, iDay);
Now here’s how we subtract 10 days from today’s date:
var today = new Date(); //Get today’s date
var interval = -10; //Setting the interval as a variable to show its applicability
var tenDaysPrior = new Date(today.getYear(), today.getMonth(), today.getDate()-0+interval);
document.write(tenDaysPrior);
And voila! No need to worry about wrapping across months or years or even bother about the number of days in any month. You could even add/subtract months/years in the same fashion. Try adding 60 days to current date. Works?
Another tip as I head off to bed, to find the number of days in any given month, here is a one liner:
var daysInMonth = new Date(iYear, iMonth-0+1, 0).getDate();
Two points to note:
– The Date object’s month runs from 0 through 11. So January would be 0, June would be 5 and December 11. The reason I am adding 1 to iMonth above is not to correct that but to infact get the 0th day of the next month, which automatically rolls back to the previous month’s last day.
Eg: To find the number of days in Feb 2008, we simply find the 0th day of March 2008. Remember that the month number of March is 2 (which coincides with our real world month number of February). I’ll let you use your brains to code that line.
– If you are wondering why I am subtracting 0 in my expressions, remember that JavaScript is not strongly typed. So to avoid ending up 1+1 with 11, I use the -0 to make sure that JavaScript treats them as numbers and not strings. One could also use *1 or /1 to the same effect.
Cheers!
I\’m sorry, but you\’re method doesn\’t work…Try adding 1 day at a time… when you reach the end of the month, then it should go to the first day of the next month. But it adds another day to the current month (ex: 32) and then the next one, it jumps right to the second day of the next month.So there is a bug at the endings and beginnings of a month…
I\’ve use a different approach:var timeObj=new Date(); // get the current datevar secsDay = 86400000; // there are 86400000 secs in 1 dayvar time = timeInput.getTime(); // number of milliseconds since midnight of January 1, 1970.var nextDay = time+86400000; // add the dayvar timeNext = new Date(nextDay); // create a new dateObjvar nDay = timeNext.getDate();if (nDay<10) nDay = \’0\’+nDay; // adds a leading 0var nMonth = timeNext.getMonth()+1;if (nMonth<10) nMonth = \’0\’+nMonth; // adds a leading 0var nYear = timeNext.getFullYear();$(\’input#date\’).val(nDay+\’-\’+nMonth+\’-\’+nYear); // fills a textbox with the new date
Don\’t see why it shouldn\’t work even for the case you mention. For the curiosity, I even tried out the situation you explained. It worked perfect for me.Look at my code:<html><body><script>function adddate(){ var someDate = new Date(2009, 0, 31); document.write(someDate + "<br/>"); var nextday = new Date(someDate.getYear(), someDate.getMonth(), someDate.getDate()+1); document.write(nextday);}</script></body><div><script>adddate()</script></div></html>Line 1 outputs Jan 31 and Line 2 adds one day to it and gets the output as Feb 1. Isnt that what is expected.My approach work and is simpler.CheersSreenath
indeed that works.. Strange, I should try it again later. I use it with a textfield and a button that adds a day and then refreches the page so I see the data off that day in my table. Perhaps there is something I overlooked…thanks for the usefull artikle! I learned mutch by it 😉
It sure does work. I just used this to make my first javascript calendar. You the man, Sreenath!Cheerz
thanx alot….saved me precious timekeep it up
yes,!!! finally, this is the one that really works, I tried all those others that ones on stackoverflow that do not work when the days cross over from a 30-day month (ie April to May) . Thank you for figuring this out.