【发布时间】:2018-12-16 11:16:05
【问题描述】:
我使用Vue.js 和Moment.js 库来处理时间,我需要比较小时和分钟,而不是日期。比如我需要比较'12:00'和'13:45',我使用.isBefore函数(docs)。
我尝试了一堆函数,它们可以处理日期,但不准确(我尝试了很多示例,所以这是最后一个)
我用moment做原型,所以$就可以了
let time = this.$moment('10:00-11:30'.split('-')[0], 'HH:mm').format('HH:mm');
let time2 = this.$moment(new Date(), 'HH:mm').format('HH:mm');
console.log({time, time2});
console.log(this.$moment(time.format('hh:mm').isBefore(this.$moment('12:00'), 'hh:mm'))
console.log(this.$moment(time, 'hh:mm').format('hh:mm').isBefore(this.$moment(time2).format('hh:mm'), 'hh:mm'))
console.log(this.$moment(this.$moment('10:00', 'HH:mm').format('HH:mm')).isBefore(this.$moment('12:00'),'HH:mm'));
console.log(this.$moment(this.$moment('10:00', 'HH:mm').format('HH:mm')).isBefore(this.$moment(time2).format('HH:mm'),'HH:mm'));
其中一些返回false,但应该返回true,其中一些返回错误.isBefore is not a function。
我还找到了this、this 和 this,但它只适用于确切的日期,而不仅仅是小时和分钟
谁能帮我弄清楚我做错了什么?
【问题讨论】: