I am getting some data from SQL which has timestamp and i Wanna check whether they are same day or not….
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…
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:
You basically create a new Date object, resetting all the time values (hours, minutes, seconds, and milliseconds).
If you’re using the
previousdateonly for this checking, you can simplify:Besides the
* 1000operation, 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.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.