-1

I'm searching a way to find the time difference between 2 dates using moment js in seconds only. I want to compare current date with dates that I'm reading from the database.

For example:

//current time
var current_time= moment().format('YYYY-MM-DD HH:mm:ss');      
//date from sql query
var starting_date=moment(element.start_date).format('YYYY-MM-DD HH:mm:ss');
// print the dates as they are now
console.log(current_time);
console.log(starting_date);

result:

2019-07-02 18:00:11
2019-05-03 15:59:29

I want to find the difference between these 2 dates in seconds only.

I try to make it work using something like this example i found:

var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b, 'days')

but it doesn't worked...

    var current_time= moment().format('YYYY-MM-DD HH:mm:ss');      
    var starting_date=moment(element.start_date).format('YYYY-MM-DD HH:mm:ss');
      current_time.diff(starting_date, 'seconds');

it crashes

Any idea how to achieve this?

John Montgomery
  • 6,739
  • 9
  • 52
  • 68
Nikos Kalantas
  • 95
  • 4
  • 11
  • 4
    How exactly did `.diff()` not work? – Pointy May 03 '19 at 13:06
  • "doesn't worked" - can you elaborate on this? did you get an empty console log? Did you get a diff of 0? Did nothing happen at all? – Lewis May 03 '19 at 13:06
  • Your example of `diff` results in `1` day which is correct for that code, but you do not show how you try to adapt this to you problem case with seconds where it does not work. You need to show **your** [mcve] try of using `diff`. – t.niese May 03 '19 at 13:07
  • @Lewis i try to say for example current_time.diff(starting_date, 'seconds'); and the result was an error "current_time.diff is not a function" – Nikos Kalantas May 03 '19 at 13:08
  • That implies current_time is not a moment object. Was it created using the code above? – Lewis May 03 '19 at 13:09
  • Using `moment.diff` you can calculate the difference in seconds by specifying you want diff in seconds. CodePen: https://codepen.io/anon/pen/NVKqEG?editors=1111 You can verify the result here: https://www.timeanddate.com/date/durationresult.html?m1=07&d1=02&y1=2019&m2=05&d2=03&y2=2019&h1=18&i1=00&s1=11&h2=15&i2=59&s2=29 – Woodrow May 03 '19 at 13:22

1 Answers1

0

You can use a.unix() - b.unix()

a.diff(b) will return diff in millisecond, you can div for 1000 to get second.

var a = moment('2019-07-02 18:00:11', 'YYYY-MM-DD HH:mm:ss');
var b = moment('2019-05-03 15:59:29', 'YYYY-MM-DD HH:mm:ss');
second = a.unix() - b.unix();  

console.log('diff with second:' + second);


secondbymin = a.diff(b);
console.log('diff with milisecond: ' + secondbymin);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.js"></script>
Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62
  • thnks but the dates that I want to compare are not in the format like a and b in the example. if you try the same for the values var current_time= moment().format('YYYY-MM-DD HH:mm:ss'); and var starting_date=moment(element.start_date).format('YYYY-MM-DD HH:mm:ss'); it doesn work – Nikos Kalantas May 03 '19 at 13:13
  • I updated answer for your format date – Hien Nguyen May 03 '19 at 13:16
  • If you want to second by diff method, you can div / 1000 – Hien Nguyen May 03 '19 at 13:27
  • did you fix your issue @NikosKalantas? – Hien Nguyen May 03 '19 at 14:52
  • yes after some tries it worked with this way: var starting_date=moment(element.start_date).format('YYYY-MM-DD HH:mm:ss'); starting_date = moment(starting_date); current_time = moment(current_time); (starting_date.diff(current_time, 'seconds') – Nikos Kalantas May 06 '19 at 06:46
  • format method will return string value, why don't use unix or diff method? – Hien Nguyen May 06 '19 at 06:47