我尝试将字符串转换为矩并检查是否相同。

    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
    }


这个控制台结果

--> 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)…}


如何转换日期以查看时间是否相同?

编辑:整个看起来像这样

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

最佳答案

尝试单独比较小时值。

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

如果您想比较整个日期,则可以比较其他值,例如minutes()

关于javascript - 使用moment在JavaScript中转换字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45312773/

10-12 13:13