Moment.js isBefore function not working as expected

前端 未结 2 1334
梦谈多话
梦谈多话 2020-12-19 13:09

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.lo         


        
相关标签:
2条回答
  • 2020-12-19 13:39

    The moment(...) argument does not accept the 'day' parameter.

    Instead, you should be calling isBefore(...) with the day parameter like so:

    moment(...).isBefore(moment(...), 'day'));
    

    More info can be found at the moment docs here.

    0 讨论(0)
  • @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.


    With that being said, you can either convert

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

    to

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

    or to

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

    Both work.


    Here is the signature for isBefore.

    0 讨论(0)
提交回复
热议问题