0

I need to check for a condition where in i need to find out if two timestamps are from separate days.

I tried using timestamp.getDate and then checking

if (currentTimestamp.getDate() > previousTimestamp.getDate() )

The problem arises when the two values would belong to two seperate months, how can I go about this in pure Javascript ?

Bazinga777
  • 5,140
  • 13
  • 53
  • 92
  • `currentTimestamp.getMonth() > previousTimestamp.getMonth() && currentTimestamp.getDate() > previousTimestamp.getDate()` ? – guest271314 Apr 15 '16 at 04:58
  • What is the type and value of "currentTimestamp"? A timestamp is usually a string representing a date and time, so unlikely to have a *getDate* method. If it's a Date, then you can compare the values of *getDate* and if `Math.abs(date0 - date1) < (2*8.64e7)`, i.e. the difference is less than two days. – RobG Apr 15 '16 at 04:59
  • 2
    Try comparing the result of .toDateString() for each date. This method returns a string that is the date part without the time, so if the result is the same you know both timestamps are from the same day. – nnnnnn Apr 15 '16 at 05:00
  • @RobG - Using the difference doesn't work, because 23:59 Monday and 00:01 Tuesday are only two minutes apart but still different days... (Or did I misunderstand what you meant?) – nnnnnn Apr 15 '16 at 05:05
  • Possible duplicate of [Compare two dates with JavaScript](http://stackoverflow.com/questions/492994/compare-two-dates-with-javascript) – tudor.gergely Apr 15 '16 at 05:06
  • @nnnnnn—yep, so comparing the dates will be false. Getting the time difference just covers where the dates are the same but in different months (or years). ;-) But your suggestion is likely best (as much I dislike the implementation specific parts of Date, this seems to be one case where it can be used). – RobG Apr 15 '16 at 05:08
  • Days *where*? Do you mean UTC days? Or days by the local time zone of the person running the script, or in some other time zone? (See [everytimezone.com](http://everytimezone.com/) for a visualization if you're unsure what I mean) – Matt Johnson-Pint Apr 15 '16 at 05:11

3 Answers3

2

There are a number of ways to achieve this.

If currentTimestamp is a string, just get the date parts (year, month, day) and compare. You haven't shown the format so how you do that is up to you.

If currentTimestamp is a date, you can use nnnnnn's suggestion and compare the date strings:

if (currentTimestamp.toDateString() == previousTimestamp.toDateString())

However since the value of toDateString is implementation dependent, should only be used within the host, strings should not be compared across hosts.

If you don't mind modifying the Date value, then:

if (currentTimestamp.setHours(0,0,0,0) == previousTimestamp.setHours(0,0,0,0))

will also return true if both are on the same day. If you don't want to change the dates, copy them first:

if (new Date(+currentTimestamp).setHours(0,0,0,0) == new Date(+previousTimestamp).setHours(0,0,0,0))

Whatever suits.

And as Matt notes, your concept of "day" should be clarified to local day or UTC day (or any other time zone) since times near the start and end of a local day are likely on different UTC (or other time zone) days, and vice versa.

RobG
  • 142,382
  • 31
  • 172
  • 209
0
var dateObj = currentTimestamp.getDate();
var month = dateObj.getUTCMonth() + 1;
//months from 1-12
var day = dateObj.getUTCDate();
var year = dateObj.getUTCFullYear();
var dateObjOld = previousTimestamp.getDate();
var dayOld = dateObjOld.getUTCDate();
var monthOld = dateObjOld.getUTCMonth() + 1;
var yearOld = dateObjOld.getUTCFullYear();
if(year > yearOld && month > monthOld && day > dayOld) {
    // currentTimeStamp is newer. Do stuff
}

See this answer https://stackoverflow.com/a/2013332/4479004

Community
  • 1
  • 1
xdevs23
  • 3,824
  • 3
  • 20
  • 33
0

Using the .toDateString() method should do it:

if (currentTimestamp.toDateString() != previousTimestamp.toDateString() ) {
    // different days
}

That method returns the date as a string without the time, in a format like "Fri Apr 15 2016" - but the specific format is irrelevant since your stated problem is just to figure out if the two timestamps are on the same day.

nnnnnn
  • 147,572
  • 30
  • 200
  • 241