This will give you the difference between two dates, in milliseconds(这将为您提供两个日期之间的差异,以毫秒为单位)
var diff = Math.abs(date1 - date2);
In your example, it'd be(在你的例子中,它是)
var diff = Math.abs(new Date() - compareDate);
You need to make sure that compareDate
is a valid Date
object.(您需要确保compareDate
是一个有效的Date
对象。)
Something like this will probably work for you(这样的事可能对你有用)
var diff = Math.abs(new Date() - new Date(dateStr.replace(/-/g,'/')));
ie turning "2011-02-07 15:13:06"
into new Date('2011/02/07 15:13:06')
, which is a format the Date
constructor can comprehend.("2011-02-07 15:13:06"
转换为new Date('2011/02/07 15:13:06')
,这是Date
构造函数可以理解的格式。) 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…