使用 momentJS 创建新日期与使用 Javascript new Date() 有什么区别?

What is the difference between creating a new date using momentJS versus using Javascript new Date()?

如果我对两个控制台进行日志记录,我会得到相同的结果,但由于某种原因,第三个控制台的结果为 false,这意味着存在一些差异!感谢任何帮助。

 console.log(new Date());
 console.log(moment(new Date()).toDate());
 console.log(moment(new Date()).toDate() == new Date());

以下是我在控制台得到的结果:

Wed Jul 08 2015 15:55:30 GMT-0500 (Central Daylight Time)
Wed Jul 08 2015 15:55:30 GMT-0500 (Central Daylight Time)
false

您正在比较 Date 个对象,但您实际上应该比较它们的 :

console.log(moment(new Date()).toDate().getTime() == (new Date()).getTime());
console.log(+(moment(new Date()).toDate()) == +(new Date()));

这里有更多讨论:Compare two dates with JavaScript