How to add days to current Date
using JavaScript. Does JavaScript have a built in function like .Net's AddDay
?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
try this
Be careful, because this can be tricky. When setting "tomorrow", it only works because it's current value matches the year and month for "today". However, setting to a date number like "32" normally will still work just fine to move it to the next month.
My simple solution is:
this solution does not have problem with daylight saving time. Also, one can add/sub any offset for years, months, days etc.
is correct code.
Thanks Jason for your answer that works as expected, here is a mix from your code and the handy format of AnthonyWJones :
I guess I'll give an answer as well:
Personally, I like to attempt to avoid gratuitous variable declaration, method calls, and constructor calls, as they are all expensive on performance. (within reason, of course)
I was going to leave this as just comment under the Answer given by @AnthonyWJones but thought better of it.
The above will respect DST. Meaning if you add a number of days that cross DST, the displayed time (hour) will change to reflect that.
Example:
Nov 2, 2014 02:00 was the end of DST.
If you're looking to retain the time across DST (so 10:30 will still be 10:30)...
So, now you have...
CodePen