-1

In javascript how can I compare two timestamps to see how many days difference they are?

Current code I have:

        var todaysDate = Math.floor(Date.now() / 1000);

        for (var i = 0; i < 1; i++) {
            var job = this.listOfJobs[i];       

            var jobDate = Date.parse(job.timestamp) / 1000;

            //now how would I compare these two timestamps to see how many days apart they are e.g. todaysDate and jobDate 
        }

The 2 timestamps I have for example are:

todays date: 1475336651 And the job created date: 1465337544

I've substracted those and it equals: 9999107

How many days is 9999107?

AngularM
  • 15,982
  • 28
  • 94
  • 169
  • 1
    Hint: You have the values in seconds, and each day contains 24 hours, each containing 3600 seconds. So subtract, get the difference in seconds, and see how many days that make. – techfoobar Oct 01 '16 at 15:37
  • this is simple math!! ...how many seconds in a minute...how many minutes in an hour...how many hours in a day?? – charlietfl Oct 01 '16 at 15:39
  • I've updated my question with the real data im getting – AngularM Oct 01 '16 at 15:46
  • 1
    Possible dublicate of [How do I get the number of days between two dates in JavaScript?](http://stackoverflow.com/q/542938/4543207) – Redu Oct 01 '16 at 15:48

2 Answers2

0

This one should do the job. I've added Math.abs in case you put a date from the future, as the 'otherDate' to avoid a negative number.

Also remember, that the first month is 0 - January.

function x(){
  var now = new Date();
  var otherDate = new Date(2015,09,01);
  return Math.abs((now - otherDate)/(1000*60*60*24));
}

And the converter: The result divided by 1000ms * 60s * 60min * 24hrs will give you the amount of days that have passed (or will).

kind user
  • 40,029
  • 7
  • 67
  • 77
-1

Subtract the two variables and store it in a new variable