-2

I am new to JavaScript and have done some research, however I cannot find a straightforward solution to relatively simple problem. I have a following var:

var recordDate = '2016-04-20 17:52:33';

I need to get the difference between now and recordDate in seconds. What would be the most efficient way to achieve it?

matisetorm
  • 857
  • 8
  • 21
WpDoe
  • 476
  • 1
  • 7
  • 22
  • 7
    Duplicate of [How Many Seconds Between Two Dates?](http://stackoverflow.com/questions/2024198/how-many-seconds-between-two-dates) – Rayon Apr 21 '16 at 10:35

2 Answers2

3

Try to convert recordDate to a Date Object first

var match = recordDate.match(/^(\d+)-(\d+)-(\d+) (\d+)\:(\d+)\:(\d+)$/);
var date = new Date(match[1], match[2] - 1, match[3], match[4], match[5], match[6]);
var currentDate = new Date();

var differenceInSeconds = Math.abs(date.getTime() - currentDate.getTime())/1000;

DEMO

var recordDate = '2016-04-20 17:52:33';
var match = recordDate.match(/^(\d+)-(\d+)-(\d+) (\d+)\:(\d+)\:(\d+)$/);
var date = new Date(match[1], match[2] - 1, match[3], match[4], match[5], match[6]);
alert(date.toString());
var currentDate = new Date();

var differenceInSeconds = Math.abs(date.getTime() - currentDate.getTime())/1000;
alert(differenceInSeconds);
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • @Guru, `new Date(STR)` will do this for us.. Any advantage of using expression over `new Date()` ? – Rayon Apr 21 '16 at 10:39
  • @RayonDabre implementation of Date() constructor is as per `Date.parse()` and as per spec http://www.ecma-international.org/ecma-262/6.0/#sec-date.parse and http://www.ecma-international.org/ecma-262/6.0/#sec-date-time-string-format (last note), it is implementation dependent http://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results – gurvinder372 Apr 21 '16 at 10:44
  • @RayonDabre if the values are invalid (invalid date), it will give `Invalid Date` output - `new Date("2234 23 33");` – gurvinder372 Apr 21 '16 at 10:45
  • @RayonDabre that is true, at least on chrome it is working fine. Just that there is no guarantee that it will work fine on all browsers (as per last link I attached) – gurvinder372 Apr 21 '16 at 10:52
  • It does not really work correct on chrome. `var date` is being set to today and not `2016-04-20`. – WpDoe Apr 21 '16 at 10:54
  • @WpDoe try the demo attached – gurvinder372 Apr 21 '16 at 10:57
  • It does indeed work now, thanks. There is just one last thing, the seconds are not rounded up to an `int`. Shoudl I do it in the `totalSeconds` calculation or afteR? – WpDoe Apr 21 '16 at 11:04
  • @WpDoe you can simply do `parseInt(differenceInSeconds)` to get the integer value out of it – gurvinder372 Apr 21 '16 at 11:06
  • I am not saying that this solution is bad, but if someone would come across this code in the app, he would swear the man who wrote the code :) If there should be a check to check if a date is valid, then there are may other valid ways like http://stackoverflow.com/questions/1353684/detecting-an-invalid-date-date-instance-in-javascript, but forming new date like this could cause many issues if somebody would like to change format or to use just a date instead. At least that is my opinion – Aleks Apr 21 '16 at 11:06
1

Set the date record like this:

var recordDate = new Date('2016-04-20 17:52:33');
var currentTime = new Date();

And then calculate it like this:

(currentTime - recordDate) / 1000 = {Seconds in between}

Additional comment:

Additional explanation why I am not using abs is because it looks like the purpose for this it to display like where in stackoverflow - edited 5 seconds ago or similar, when the currentTime will never be less then the record date and therefore one calculation and dependency less.

Aleks
  • 4,866
  • 3
  • 38
  • 69