简体   繁体   English

如何以分钟为单位计算两个日期之间的时差

[英]How can I calculate difference between two dates with time in minutes

I'm working with javascript and I have dates in this format "2016-04-15 1230" (yyyy-MM-dd HHmm) and I want to calculate the difference between these dates in minutes. 我正在使用JavaScript,并且我的日期格式为“ 2016-04-15 1230”(yyyy-MM-dd HHmm),我想以分钟为单位计算这些日期之间的差异。

Example : 范例:

The difference between "2016-04-15 1230" and "2016-04-15 1210" is 20 minutes “ 2016-04-15 1230”和“ 2016-04-15 1210”之间的差是20分钟

moment.js is a good library for this sort of thing: moment.js是这类事情的一个不错的图书馆:

var firstTime = moment("2016-04-15 1230", "YYYY-MM-DD HHmm");
var secondTime = moment("2016-04-15 1210", "YYYY-MM-DD HHmm");

var difference = moment.duration(firstTime - secondTime);

console.log(difference.asMinutes()); // 20

First you need to get the Date representation of your date values. 首先,您需要获取日期值的Date表示形式。

/* Helper function that parses date string (format DD-YY-MM HH:mm) 
   to create a JS Date object*/
function getDateFromDateStr(dateStr) {
  var dateParts=dateStr.split(/\D/);
  return new Date(dateParts[0],dateParts[1],dateParts[2],dateParts[3],dateParts[4]);
}

...
... 
/*Use input string to create date objects*/
var str1= "2016-04-15 1210"; 
var str2= "2016-04-15 1230";

var len = str1.length;
var date1 = getDateFromDateStr(str1.slice(0,len-2) + ":" + str1.slice(len-2));
len = str2.length;
var date2 = getDateFromDateStr(str2.slice(0,len-2) + ":" + str2.slice(len-2));

Then you can simply get the difference in milliseconds by using simple subtraction and convert the result in minutes: 然后,您可以使用简单的减法来简单地得到毫秒数的差,并将结果转换为分钟:

var differenceInMinutes = (date2 - date1)/(60*1000);

NOTE - Please keep in mind is that you should ONLY attempt arithmetic operations on Dates in UTC. 注意-请记住,您只能尝试对UTC中的日期进行算术运算。 Or if you can guarantee that they'll always be in the same time zone and won't have any DST. 或者,如果您可以保证它们将始终处于同一时区并且没有任何DST。

a = new Date('2016-07-21 12:30'); a = new Date('2016-07-21 12:30'); b = new Date('2016-07-21 12:20'); b =新日期(“ 2016-07-21 12:20”);

console.log((ab)/60000); console.log((ab)/ 60000);

prints 10. 打印10。

Also see How to add 30 minutes to a JavaScript Date object? 另请参见如何向JavaScript Date对象添加30分钟?

Thanks @RobG for correcting me. 感谢@RobG纠正我。 Here is another go at it. 这是另一回事了。 I tried it with the time example given and changing both the minutes and hours of both times. 我用给出的时间示例进行了尝试,并更改了两个时间的分钟和小时。 I'm sure this isn't the fastest way but it is a homebrew. 我敢肯定这不是最快的方法,但它是自制的。

var first = "2016-04-15 1230"

var second ="2016-04-15 1210"


var timeDiff = function(x, y) {

// Gets first hours and cuts it out
hrs1 = first.slice(11, 13);

// Gets second hours and cuts it out
hrs2 = second.slice(11, 13);

// Gets the minutes from first time and cuts it out
min1 = first.slice(13,15);

// Gets the minutes from second time and cuts it out
min2 = second.slice(13,15);

// Subtracts and puts the difference of minutes in minDiff variable
var minDiff = min1 - min2;

// Subtracts and puts the difference of hours in hrsDiff variable  
var hrsDiff = hrs1 - hrs2;

//  If it is negative, make it positive
if(minDiff <0){
minDiff *= -1;
}

if(hrsDiff < 0) {

  hrsDiff *= -1;

}

return ["Diff in hrs: " + hrsDiff, "Difference in Minutes: " + minDiff];

}

Returns: ["Diff in hrs: 0", "Difference in Minutes: 20"] 返回:[“以小时为单位的时差:0”,“以分钟为单位的时差:20”]

With times: "2016-04-15 1530", and "2016-04-15 1248", 与时间:“ 2016-04-15 1530”和“ 2016-04-15 1248”,

Returns: ["Diff in hrs: 3", "Difference in Minutes: 18"] 返回值:[“以小时为单位的时差:3”,“以分钟为单位的时差:18”]

Hope this was helpful. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Javascript 中正确计算这两个日期之间的分钟数? - How can I correctly calculate the number of minutes between these two dates in Javascript? 计算两个时间值之间的时间差(以分钟为单位) - Calculate the time difference in minutes between two time values 如何计算JavaScript中两个日期之间的年,月和天的差异? - How can I calculate the difference in years, months and days between two dates in Javascript? 如何计算不同日期的 24 小时格式的两个时间之间的差异? - How can I calculate the difference between two times that are in 24 hour format which are in different dates? 计算两个日期之间的差 - calculate the difference between two dates 如何在javascript中获取两个原始日期之间的分钟数差异? - How to obtain difference in minutes between two raw dates in javascript? 如何计算两个日期之间的年数? - How can I calculate the number of years between two dates? 在几分钟内获得两个日期之间的差 - Get the Difference Between Two Dates in Minutes JavaScript计算年,月,日,小时,分钟的两个日期之间的差 - JavaScript calculate difference between two dates, in Years, Months, Days, Hours, Minutes 计算两个日期格式(YYYY-MM-DD HH:MM)之间的差异,并以分钟为单位显示结果 - Calculate the difference between two dates of format (YYYY-MM-DD HH:MM) and display results in minutes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM