javascript - Moment.js diff of two moments defined by week and weekday gives wrong result -
i want calculate difference between moment (itemmoment) , (nowmoment) defined weekday, hour , minute. if itemmoment before nowmoment (diff < 0) want add 1 week , calculate difference again, addition of week (week: week + 1 in code )somehow doesn't change difference , still negative difference.
it's first time use moment.js library may don't how use correctly. great if experienced used help.
var = moment(); var year = now.year(); var week = now.week(); var weekday = now.weekday(); var hour = now.hour(); var minute = now.minute(); var itemmoment = moment({ day: item.weekday, hour: item.hour, minute: item.minute }); var nowmoment = moment({ day: weekday, hour: hour, minute: minute }); if (itemmoment.diff(nowmoment) > 0) { item.date = moment({ year: year, week: week, weekday: item.weekday, hour: item.hour, minute: item.minute }); diff = itemmoment.diff(nowmoment); } else { if (week == 51) { week = -1; year = year + 1 } item.date = moment({ year: year, week: week + 1, weekday: item.weekday, hour: item.hour, minute: item.minute }); diff = item.date.diff(now);
you can comparison using isbefore() or isafter(). http://momentjs.com/docs/#/query/
if (itemmoment.isbefore(nowmoment)) {
then can manipulation so: http://momentjs.com/docs/#/manipulating/
item.date = itemmoment.add('weeks', 1);
Comments
Post a Comment