两个日期之间的差异(日期时间),以分钟为单位[重复]

时间:2022-05-08 02:58:41

This question already has an answer here:

这个问题在这里已有答案:

I need to check difference between two dates - from db and actual, but in minutes in JS/jQuery (I need to run function to check by ajax).
This is format of my date:

我需要检查两个日期之间的差异 - 从db和actual,但在JS / jQuery中需要几分钟(我需要运行函数来检查ajax)。这是我约会的格式:

19-01-2016 22:18

I need something like this:

我需要这样的东西:

if ( (date_actual - date_db) < 1 minute ) {
   //do something
}

How can I do this?

我怎样才能做到这一点?

EDIT: I created something like this, but I always getting same log, it's not change - http://jsbin.com/xinaki/edit?js,console

编辑:我创建了这样的东西,但我总是得到相同的日志,它不会改变 - http://jsbin.com/xinaki/edit?js,console

EDIT 2: Here it's working code, BUT now it's checking in the same hour/minute, ex. 12:50:50 and 12:50:58 show log only for this 2 seconds, but I need to 'stay' log on 1 minute
http://jsbin.com/laruro/edit?js,console

编辑2:这是工作代码,但现在它以相同的小时/分钟进行检查,例如。 12:50:50和12:50:58只显示这2秒的日志,但我需要'保持'登录1分钟http://jsbin.com/laruro/edit?js,console

2 个解决方案

#1


2  

Assuming all dates are in the same format, and that they are strings, you could use this helper function to convert one or both of the strings to a real Date() object:

假设所有日期都是相同的格式,并且它们是字符串,您可以使用此辅助函数将一个或两个字符串转换为真正的Date()对象:

var makeDate = function(dateString) {
  var d = dateString.split(/[\s:-]+/);
  return new Date(d[2],d[1] - 1,d[0],d[3],d[4]);
}

And then use it like so to compare a string to the current date:

然后像这样使用它来比较字符串和当前日期:

var diffInMin = function(dateString) {
  return ( new Date() /* < current date */ - makeDate(dateString) ) / ( 1000 * 60 );
};

or like this to compare two date strings:

或者像这样来比较两个日期字符串:

var diffInMin = function(dateString1, dateString2) {
  // this assumes date string one will always be more recent than DateString2
  return ( makeDate(dateString1) - makeDate(dateString2) ) / ( 1000 * 60 ); 
};

#2


0  

Try this function

试试这个功能

function diffInMin(dateString1, dateString2){
  return (+ new Date(dateString2) - new Date(dateString1))/(1000*60)
}

#1


2  

Assuming all dates are in the same format, and that they are strings, you could use this helper function to convert one or both of the strings to a real Date() object:

假设所有日期都是相同的格式,并且它们是字符串,您可以使用此辅助函数将一个或两个字符串转换为真正的Date()对象:

var makeDate = function(dateString) {
  var d = dateString.split(/[\s:-]+/);
  return new Date(d[2],d[1] - 1,d[0],d[3],d[4]);
}

And then use it like so to compare a string to the current date:

然后像这样使用它来比较字符串和当前日期:

var diffInMin = function(dateString) {
  return ( new Date() /* < current date */ - makeDate(dateString) ) / ( 1000 * 60 );
};

or like this to compare two date strings:

或者像这样来比较两个日期字符串:

var diffInMin = function(dateString1, dateString2) {
  // this assumes date string one will always be more recent than DateString2
  return ( makeDate(dateString1) - makeDate(dateString2) ) / ( 1000 * 60 ); 
};

#2


0  

Try this function

试试这个功能

function diffInMin(dateString1, dateString2){
  return (+ new Date(dateString2) - new Date(dateString1))/(1000*60)
}