Context: An Event Planning/Management Software Tool
Goal: To clone an event from a year previous - and provide the user a head start on selecting the start & end dates of the event.
Assumption: This new event is likely a repeat/year-over-year event. So, an Oct 2018 event would be cloned for Oct 2019.
Example: Event being cloned had a start date of: Thu, Oct 20, 2016 and an end date of: Thu, Nov 3, 2016 (this includes setup and teardown dates for the event)
Expected Result:
- Number of days in date range is maintained. (14 days)
- Day of the Week is maintained. (Thursday for both)
The code should return a new start date of: Thu, Oct 17, 2019 and an end date: Thu, Oct 31, 2019
Optionally: Another acceptable date range might be: Thu, Oct 24, 2019 and an end date of Nov 7, 2019
Possible logic: I think the expected result could be achieved by grabbing the month, week of that month, and day of that week - and constructing the new event date off of that, for the new year.
Considerations: I want to use the Moment.js library if at all possible
Current Code: This is what we currently have, but it is not returning the expected result. Seems to just be subtracting a day regardless of the year difference.
const today = moment();
const thisYear = today.format('YYYY');
const old_start = moment('2018-10-08');
const old_end = moment('2018-08-12');
// if in same year, use current dates
if (thisYear === old_start.format('YYYY')) {
console.log({
start: new_start.format('YYYY-MM-DD'),
end: new_end.format('YYYY-MM-DD')
})
} else {
console.log({
start: new_start.year(thisYear)
.isoWeek(old_start.isoWeek())
.isoWeekday(old_start.isoWeekday())
.format('YYYY-MM-DD'),
end: new_end.year(thisYear)
.isoWeek(old_end.isoWeek())
.isoWeekday(old_end.isoWeekday())
.format('YYYY-MM-DD')
})
}
The resulting, incorrect dates:
Would appreciate any help on what we're doing wrong here - and how we can fix it. Thanks!