Anyone familiar with MomentJS? What’s a better way to do this? It works, but I want it to work cleaner and without the substr’s which I really feel don’t need to be there.
/**
* Sets the next instance of a weekly event
* @param {numbner} day - day of week
* @param {string} time - 24hr time
* @return {object} momentjs object of next occurance
*/
getNextAir(day, time) {
let _hour = time.substr(0, time.indexOf(':')),
_min = time.substr(time.indexOf(':')+1, time.length);
if(day < Moment().utc().day()) {
day += 7;
}
return Moment().utc().weekday(day).hour(_hour).minute(_min).seconds(0);
}
I’ve tried Moment(day +' '+ time, 'e HH:mm'), but that was setting it to the first day of that week in the year. I can’t seem to find an example of what I’m trying to do in the documentation. And this method is the right way, instead of the one above, but I’m missing something.
I can think of other ways, but TBH I wouldn’t consider them “cleaner”
A regex pattern would work, but for the additional overhead, why use it?
The time value could be converted to Unix time, but would need to be converted back.
I guess a lot depends on what you need to do with it. If it’s just to get Day, Hour and Minute returned in “human readable” form I think Stephen’s example is about as clean as you’re going to get.