qcreator qcreator - 3 years ago 110
Javascript Question

Convert string in javascript using moment

i try convert string to moment and check is same.

protected showEvent(event: IEvent, hour: Moment): boolean {
let formatDate: Moment = moment(event.futureDate);
console.log('--> formatDate', formatDate);
console.log('--> hour', hour);

return formatDate.isSame(hour, "hour"); // return true is same

this console result

--> formatDate: Moment {_isAMomentObject: true, _i: "07-24-2017 07:00:00.000", _isUTC: false, _pf: Object, _locale: Locale…}_d: Mon Jul 24 2017 07:00:00 GMT+0200 (CEST)_i: "07-24-2017 07:00:00.000"}

--> hour: Moment {_isAMomentObject: true, _isUTC: false, _pf: Object, _locale: Locale, _d: Tue Jul 25 2017 07:00:00 GMT+0200 (CEST)…}

How can I convert the date to see if the time is the same?

edit: the whole looks like this

protected showEvent(event: IEvent, hour: Moment): boolean {
return moment(event.futureDate).hour() == hour.hours()

Answer Source

Try comparing the hour value alone.

return formatDate.hours() == hour.hours()

If you are looking to compare the entire date you can compare the other values e.g. minutes()

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download