I need to increment a date value by one day in JavaScript.
For example, I have a date value 2010-09-11 and I need to store the date of the next day in a JavaScript variable.
How can I increment a date by a day?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Three options for you:
1. Using just JavaScript’s
Dateobject (no libraries):My previous answer for #1 was wrong (it added 24 hours, failing to account for transitions to and from daylight saving time; Clever Human pointed out that it would fail with November 7, 2010 in the Eastern timezone). Instead, Jigar’s answer is the correct way to do this without a library:
This works even for the last day of a month (or year), because the JavaScript date object is smart about rollover:
2. Using MomentJS:
(Beware that
addmodifies the instance you call it on, rather than returning a new instance, sotoday.add(1, 'days')would modifytoday. That’s why we start with a cloning op onvar tomorrow = ....)3. Using DateJS, but it hasn’t been updated in a long time: