简体   繁体   English

检查两个时间戳是同一天

[英]check two timestamps are same day

I am getting some data from SQL which has timestamp and i Wanna check whether they are same day or not.... 我从SQL中获取了一些有时间戳的数据,我想检查它们是否是同一天......

In a loop i am trying to check the previous loop timestamp is same as the current loop timestamp...... I am trying something like this..... 在循环中我试图检查前一个循环时间戳与当前循环时间戳相同......我正在尝试这样的事情......

$.each(data, function(key, value) {
   if( new Date([value.startTime] * 1000) === previousdate){
       console.log("*** Same day ***");
    }
    previousdate = new Date([value.startTime] * 1000);
}

But though they are same day but they differ in time ...... I just want to check they both are same day or not..... I can trim them before comparing them so that way it works fine.... but is there any other effective way to do this..... 但是虽然它们是同一天,但它们在时间上有所不同......我只想检查它们是否在同一天......我可以在比较它们之前修剪它们以便它可以正常工作......但有没有其他有效的方法来做到这一点......

Cheers... 干杯...

You can also try checking their date strings. 您也可以尝试检查他们的日期字符串。

var isSameDay = function(date, otherDate) {
  return date.toDateString() === otherDate.toDateString();
};

This is nice because it's pretty easy to do inline. 这很好,因为内联很容易。

It's not clear to me if you want to check if it's the same day or the same date: you talk about same day but from what you're doing seems your want to check if it's the same date. 我不清楚你是否想要检查它是在同一天还是同一天:你谈论的是同一天但是从你正在做的事情看来你想要检查它是否是同一天。 A way to check if it's the same date, is the follow: 检查是否是相同日期的方法如下:

$.each(data, function(key, value) {
    var current = new Date(value.startTime * 1000).setHours(0, 0, 0, 0);
    var previous = new Date(previousDate).setHours(0, 0, 0, 0);

    if(current === previous){
        console.log("*** Same day ***");
    }
    previousdate = new Date([value.startTime] * 1000);
})

You basically create a new Date object, resetting all the time values (hours, minutes, seconds, and milliseconds). 您基本上创建一个新的Date对象,重置所有时间值(小时,分钟,秒和毫秒)。

If you're using the previousdate only for this checking, you can simplify: 如果您仅使用previousdate进行此检查,则可以简化:

$.each(data, function(key, value) {
    var current = new Date(value.startTime * 1000).setHours(0, 0, 0, 0);

    if(current === previousdate){
        console.log("*** Same day ***");
    }

    previousdate = current;
});

Besides the * 1000 operation, that is the easiest and safest way AFAIK. 除了* 1000操作,这是AFAIK最简单,最安全的方式。 You let the engine doing all the calculation: not all days are made by 24 hours – see the daylight saving time – and not all the minutes are made by 60 seconds – see the leap second; 你让引擎进行所有的计算:不是所有的日子都是24小时 - 看夏令时 - 而不是所有的分钟都是60秒 - 见闰秒; so it's no safe make any operation assuming that. 所以假设这样做是不安全的。

Of course some engine could not deals with leap second as well, for instance, but it's still better, at least for the daylight saving time. 例如,当然一些发动机也不能处理闰秒,但它仍然更好,至少在夏令时。

Since you only care about the UTC date, this should be easy. 由于您只关心UTC日期,因此应该很容易。 You timestamp is in number of seconds, so if you divide by the number of seconds in a day and then floor the value, you should get the number of days since Jan 1, 1970. If both values are the same number of days since 1970, then you have a match. 您的时间戳以秒为单位,因此如果您除以一天中的秒数然后将该值置于最低值,您应该获得自1970年1月1日以来的天数。如果两个值都是自1970年以来的相同天数那你就匹配了。

$.each(data, function(key, value) {
    var date, previous;
    date = Math.floor(value.startTime / 60 / 60 / 24);
    if (date === previous){
       console.log("*** Same day ***");
    }
    previous = date;
}

For future googlers, this is what worked for me: 对于未来的googlers,这对我有用:

function isSameDay(firstTimeStamp, secondTimeStamp) {
    return (new Date(firstTimeStamp).getDate() === (new Date(secondTimeStamp).getDate());
}

Note: your timestamps need to be in milliseconds. 注意:您的时间戳需要以毫秒为单位。 If they are in seconds, you will need to adjust them. 如果它们在几秒钟内,您将需要调整它们。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM