Moment.js isBefore函数无法按预期运行 [英] Moment.js isBefore function not working as expected

查看:1199
本文介绍了Moment.js isBefore函数无法按预期运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的控制台日志给了我意外的输出.

My console log is giving me an unexpected output.

var bool = (moment("2017-04-08 23:00:00").isBefore(moment("2017-04-09 01:00:00", 'day')));
console.log(bool);

由于某种原因,输出为false. 根据文档,以下代码应返回true.

The output is false, for some reason. According to the documentation, the following code should return true.

moment('2010-10-20').isBefore('2011-01-01', 'year')

即使不是整整一年,如果是不同的年份,我的理解是它应该返回false. 就我而言,虽然还没有24小时,但那是另一天. 有什么我不能正确理解的东西吗?

Even if it's not a full year past, if it's a different year, my understanding is that it should return false. In my case, while it hasn't yet been 24 hours, it is a different day. Is there something I'm not understanding correctly?

推荐答案

@Oliver Charlesworth 是正确的,moment()不接受'day'作为第二个参数.在此处进行浏览,并向下滚动以获取其所有有效签名.

@Oliver Charlesworth is right, moment() doesn't accept 'day' as a second argument. Have a look here and scroll down for all its valid signatures.

话虽如此,您可以转换

isBefore(moment("2017-04-09 01:00:00", 'day'));

isBefore(moment('2017-04-09 01:00:00'), 'day');

isBefore('2017-04-09 01:00:00', 'day');

两个工作.

此处是isBefore的签名.

Here is the signature for isBefore.

这篇关于Moment.js isBefore函数无法按预期运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆