javascript 比较 2 个 ISO 8601 时间戳和输出秒/分钟差异

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18023857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 10:31:05  来源:igfitidea点击:

Compare 2 ISO 8601 timestamps and output seconds/minutes difference

javascript

提问by jskidd3

I need to write JavaScript that's going to allow me to compare two ISO timestamps and then print out the difference between them, for example: "32 seconds".

我需要编写允许我比较两个 ISO 时间戳的 JavaScript,然后打印出它们之间的差异,例如:“32 秒”。

Below is a function I found on Stack Overflow, it turns an ordinary date into an ISO formatted one. So, that's the first thing out the way, getting the current time in ISO format.

下面是我在 Stack Overflow 上找到的一个函数,它将普通日期转换为 ISO 格式的日期。所以,这是第一件事,以 ISO 格式获取当前时间。

The next thing I need to do is get another ISO timestamp to compare it with, well, I have that stored in an object. It can be accessed like this: marker.timestamp (as shown in the code below). Now I need to compare those two two timestamps and work out the difference between them. If it's < 60 seconds, it should output in seconds, if it's > 60 seconds, it should output 1 minute and 12 seconds ago for example.

接下来我需要做的是获取另一个 ISO 时间戳来与它进行比较,好吧,我已经将它存储在一个对象中。它可以像这样访问:marker.timestamp(如下面的代码所示)。现在我需要比较这两个时间戳并找出它们之间的差异。如果小于 60 秒,则应以秒为单位输出,如果大于 60 秒,则应输出例如 1 分 12 秒前。

Thanks!

谢谢!

function ISODateString(d){
 function pad(n){return n<10 ? '0'+n : n}
 return d.getUTCFullYear()+'-'
      + pad(d.getUTCMonth()+1)+'-'
      + pad(d.getUTCDate())+'T'
      + pad(d.getUTCHours())+':'
      + pad(d.getUTCMinutes())+':'
      + pad(d.getUTCSeconds())+'Z'}

var date = new Date();
var currentISODateTime = ISODateString(date);
var ISODateTimeToCompareWith = marker.timestamp;

// Now how do I compare them?

回答by Paul S.

Comparing two dates is as simple as

比较两个日期很简单

var differenceInMs = dateNewer - dateOlder;

So, convert the timestamps back into Dateinstances

因此,将时间戳转换回Date实例

var d1 = new Date('2013-08-02T10:09:08Z'), // 10:09 to
    d2 = new Date('2013-08-02T10:20:08Z'); // 10:20 is 11 mins

Get the difference

获取差异

var diff = d2 - d1;

Format this as desired

根据需要格式化

if (diff > 60e3) console.log(
    Math.floor(diff / 60e3), 'minutes ago'
);
else console.log(
    Math.floor(diff / 1e3), 'seconds ago'
);
// 11 minutes ago

回答by seanmk

I would just store the Date object as part of your ISODate class. You can just do the string conversion when you need to display it, say in a toStringmethod. That way you can just use very simple logic with the Date class to determine the difference between two ISODates:

我只是将 Date 对象存储为 ISODate 类的一部分。您可以在需要显示字符串时进行字符串转换,例如在toString方法中。这样你就可以在 Date 类中使用非常简单的逻辑来确定两个 ISODate 之间的差异:

var difference = ISODate.date - ISODateToCompare.date;
if (difference > 60000) {
  // display minutes and seconds
} else {
  // display seconds
}

回答by federico-t

I'd recommend getting the time in seconds from both timestamps, like this:

我建议从两个时间戳中获取以秒为单位的时间,如下所示:

// currentISODateTime and ISODateTimeToCompareWith are ISO 8601 strings as defined in the original post
var firstDate = new Date(currentISODateTime),
    secondDate = new Date(ISODateTimeToCompareWith),
    firstDateInSeconds = firstDate.getTime() / 1000,
    secondDateInSeconds = secondDate.getTime() / 1000,
    difference = Math.abs(firstDateInSeconds - secondDateInSeconds);

And then working with the difference. For example:

然后使用difference. 例如:

if (difference < 60) {
    alert(difference + ' seconds');
} else if (difference < 3600) {
    alert(Math.floor(difference / 60) + ' minutes');
} else {
    alert(Math.floor(difference / 3600) + ' hours');
}

Important: I used Math.absto compare the dates in seconds to obtain the absolute difference between them, regardless of which is earlier.

重要提示:我曾经Math.abs以秒为单位比较日期以获得它们之间的绝对差异,无论哪个更早。