javascript 检查两个时间戳是同一天
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13763652/
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
check two timestamps are same day
提问by troy
I am getting some data from SQL which has timestampand 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...
干杯...
回答by vpontis
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.
这很好,因为它很容易内联。
回答by ZER0
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
only 进行此检查,则可以简化:
$.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. 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; so it's no safe make any operation assuming that.
除了* 1000
操作,这是AFAIK最简单和最安全的方法。你让引擎做所有的计算:并不是所有的日子都是 24 小时制的——参见夏令时——并不是所有的分钟都是 60 秒制的——参见闰秒;所以假设这样进行任何操作是不安全的。
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.
当然,某些引擎也无法处理闰秒,例如,但它仍然更好,至少对于夏令时而言。
回答by Nathan Wall
Since you only care about the UTC date, this should be easy. 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.
由于您只关心 UTC 日期,因此这应该很容易。您的时间戳以秒为单位,因此如果您除以一天中的秒数,然后将该值下限,您应该得到自 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;
}
回答by Kenny Grage
For future googlers, this is what worked for me:
对于未来的谷歌员工,这对我有用:
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.
注意:您的时间戳需要以毫秒为单位。如果它们以秒为单位,您将需要调整它们。