I'm creating an application which lets you define events with a time frame. I want to automatically fill in the end date when the user selects or changes the start date. I can't quite figure out, however, how to get the difference between the two times, and then how to create a new end Date using that difference.
相关问题
- 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?
If you don't care about the time component, you can use
.getDate()
and.setDate()
to just set the date part.So to set your end date to 2 weeks after your start date, do something like this:
To return the difference (in days) between two dates, do this:
Finally, let's modify the first function so it can take the value returned by 2nd as a parameter:
Thanks @Vincent Robert, I ended up using your basic example, though it's actually
newBegin + oldEnd - oldBegin
. Here's the simplified end solution:Depending on your needs, this function will calculate the difference between the 2 days, and return a result in days decimal.
THIS IS WHAT I DID ON MY SYSTEM.