0

I am trying to calculate the difference between NOW and when an item was posted. How can I do this? Currently I am only getting monster big results which are not correct..

Goal:

How many seconds ago was this posted?

var thisTime = jQuery.now()/1000;
var postTime = new Date('2014-07-30 07:32:22')/1000;
var timeDiff = thisTime - postTime;

var text = timeDiff +'seconds ago';

Should return something like: timeDiff seconds ago.

Nabil
  • 175
  • 1
  • 2
  • 10

1 Answers1

1

Here is your answer:

var thisTime = new Date().getTime()/1000;
var postTime = new Date('2014-08-01 12:20:22').getTime()/1000;
var timeDiff = thisTime - postTime;

var text = timeDiff +' seconds ago';
console.log(text);

Date.getTime() returns milliseconds since 1970/01/01 - to get seconds you have to divide by 1000. Then, you simply use substraction.

Derek Wang
  • 10,098
  • 4
  • 18
  • 39
dstronczak
  • 2,406
  • 4
  • 28
  • 41