I am building a feature into my app that allows an event to scheduled into the future. IE: The event takes place every Wednesday at 6:30pm. I need to show the user, based on today's current date, when the NEXT Wednesday is.
If today is Thu Dec 6th 2018, it needs to show the user the next event is on "Wed Dec 12th, 2018 at 6:30pm"
If today is "Tuesday Jan 8th, 2019", it needs to show the user the next event is on "Wed Jan 9th, 2019 at 6:30pm"
The original start date is of the event is captured in my DB as the first day of the ongoing series. IE: Wed Sept 10, 2018. I am using that start date to try and calculate every next Wednesday.
function nextEvent(cEvent) {
var startDate = cEvent.ceDate+ ", " +cEvent.ceTime ;
var ceDate = new Date(startDate) ;
var curDate = new Date() ;
var timeDiff = Math.abs(curDate.getTime() - ceDate.getTime()) ;
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)) ;
var addDays = Math.ceil(diffDays / 7) * 7 ;
var futureDate = ceDate.setDate(ceDate.getDate() + addDays) ;
var futureEvent = ceDate.format('D, M d, Y @ g:i a') ;
return futureEvent ;
}
}
// this date object START event is Wed Sept 10, 2018
var dObject = {"ceDate":"2018-09-10","ceTime":"18:30:00"}
var nextDate = nextEvent(dObject) ;
I am looping through several events, all using different start dates (ie: ceDate), and NEXT event is displaying the wrong next day. The example above (hard coded in dObject) is displaying Mon Dec 10, 2018
, when it should be displaying Wed Dec 12, 2018
.
As well, if today is Wednesday at 6pm, the next event still needs to show as TODAY at 6:30pm. The event lasts minimum 2 hours...so ideally I would also like to figure out how to show something like: Next Event: happening now til 8:30pm