/    Sign up×
Community /Pin to ProfileBookmark

Adding a day to a date in javascript???

I’ve got a somewhat unique problem. I need to write a function that accepts a date, and adds a day to it. This gets difficult because of the # of days in a month. I’ve started writing a function that’s extremely messy and inefficient, so I was hoping I could get some input on the correct or best way to do this…

Example of what I’ve got started (psuedo-code):

function AlterDate(oldDate){

//if oldDate’s month = december, and day = 31, set month=1, day=1, increment year.

//elseif oldDate’s month has 30 days, and day=30, increment month and set day to 1

//elseif oldDate’s month is= feb, and day=28, increment month and set day =1.

//elseif oldDate’s month has 31 days, and day=31, increment month and set day to 1

//else increment day.

return newDate;

}

So for example, if 10/31/2009 is passed in, I need to return 11/01/2009.

jan 31 feb 28/29 mar 31 apr 30 may 31 jun 30
jul 31 aug 31 sep 30 oct 31 nov 30 dec 31

Thanks!

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@KorNov 13.2009 — The javascript interpretor connects the CPU's clock and calendar, thus it will [I]automatically[/I] adjust data whenever you add a day (or more). You must not care about the number of the day in a month or leap years. All you have to do is to play with the methods of the object Date()

See also:http://www.w3schools.com/js/js_obj_date.asp

Rude code:
<i>
</i>&lt;script type="text/javascript"&gt;
var date=new Date(2012,1,28);
var dd=date.getDate();
date.setDate(dd+1);
alert(date);
&lt;/script&gt;

Well, the above is not really enough, as the CPU might be instructed to adjust the time according to the DST, thus for the dates when the DST is applied in certain countries, the code might bring errors. To prevent that, set your date at a different hour that 00:00 (which is the default, if no hour is specified), let's say 01:00. You may subtract that hour lately, if you need a precise hourly calendar
<i>
</i>&lt;script type="text/javascript"&gt;
var date=new Date(2012,1,28,[B][COLOR="Blue"]1[/COLOR][/B]);
var dd=date.getDate();
date.setDate(dd+1);
alert(date);
&lt;/script&gt;
Copy linkTweet thisAlerts:
@mcruauthorNov 16.2009 — thanks,

the only thing is that I need the date formatted as "year/month/day"
×

Success!

Help @mcru spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...