JavaScript筛选器帮助程序中的精确字符串匹配未返回true

JavaScript筛选器帮助程序中的精确字符串匹配未返回true,javascript,date,date-fns,Javascript,Date,Date Fns,我正在使用过滤器帮助器创建一个新的项目数组,这些项目与今天的日期相匹配。我已经确认,这两个比较都是长度为10的字符串,但是在过滤器中进行比较时,它们不会被识别为匹配项 我知道这与new Date()方法有关,因为当我对日期字符串进行测试时,第一次比较是有效的,而new Date方法不起作用 if(chosenDate === 'today') { // this does not work even though both values are equal const s

我正在使用过滤器帮助器创建一个新的项目数组,这些项目与今天的日期相匹配。我已经确认,这两个比较都是长度为10的字符串,但是在过滤器中进行比较时,它们不会被识别为匹配项

我知道这与new Date()方法有关,因为当我对日期字符串进行测试时,第一次比较是有效的,而new Date方法不起作用

if(chosenDate === 'today') {
      // this does not work even though both values are equal
      const scheduled = this.props.scheduled.filter(event => event.scheduled_at.substring(0, 10) === new Date(), 'yyyy-MM-dd');

      // this works
      const scheduled = this.props.scheduled.filter(event => event.scheduled_at.substring(0, 10) === '2019-11-14');

      // this does not work
      const scheduled = this.props.scheduled.filter(event => '2019-11-14' === new Date(), 'yyyy-MM-dd');
      console.log(scheduled)
    }

    console.log(this.props.scheduled[0].scheduled_at.substring(0, 10));
    console.log(dateFnsFormat(new Date(), 'yyyy-MM-dd'));

为什么新日期字符串的比较不相等?

这似乎是你的意思

const scheduled = this.props.scheduled.filter(event =>
  event.scheduled_at.substring(0, 10) === dateFnsFormat(new Date(), 'yyyy-MM-dd')
);
但是写

const scheduled = this.props.scheduled.filter(event =>
  event.scheduled_at.substring(0, 10) === new Date()
, 'yyyy-MM-dd'
);

好像你是说

const scheduled = this.props.scheduled.filter(event =>
  event.scheduled_at.substring(0, 10) === dateFnsFormat(new Date(), 'yyyy-MM-dd')
);
但是写

const scheduled = this.props.scheduled.filter(event =>
  event.scheduled_at.substring(0, 10) === new Date()
, 'yyyy-MM-dd'
);

什么是日期格式?这与这个问题有关系吗?为什么要将
'yyyy-MM-dd'
作为第二个参数传递给
过滤器()
?这样可以更容易地格式化日期。第二个参数使用dateFnsFormat格式化日期。请在OP中包含相关标记和注释。什么是
dateFnsFormat
?这与这个问题有关系吗?为什么要将
'yyyy-MM-dd'
作为第二个参数传递给
过滤器()
?这样可以更容易地格式化日期。第二个参数使用dateFnsFormat格式化日期。请在OP中包含相关标记和注释。哦,天哪……我真是太傻了。非常感谢你的帮助!哦,天哪……我真是太傻了。非常感谢你的帮助!