-1

I need to find difference between 2 dates in seconds , i am passing one date from front end and another date is current date but i am getting error

here is my code in front end

 var a =moment().format('MM/DD/YYYY HH:mm:ss')

here is nodeJs code that I need to compare

var sesdate=moment(request.body.a).format("DD/MM/YYYY HH:mm:ss")
    var startDate = moment(sesdate, "DD/MM/YYYY HH:mm:ss");
    var currenDate = moment(new Date()).format("DD/MM/YYYY HH:mm:ss");
    var endDate = moment(currenDate, "DD/MM/YYYY HH:mm:ss");
    var result = 'Diff: ' + endDate.diff(startDate, 'seconds');

but i am getting expected output but getting warning message as

Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are discouraged. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.

vellai durai
  • 1,019
  • 3
  • 16
  • 38

1 Answers1

0

If you already have two JavaScript DateTime objects, you can use the .getTime() method to get the milliseconds since 1970. Subtract one from the other and divide by 1000 to convert to seconds. You may need to take the absolute value if it's not guaranteed that one of them is always after the other.

Alternatively, if you want to stick to your moment code, you'll need to convert your time format to one that is RFC2822 / ISO compatible. The top answer to this question will show you how to do that:

j s
  • 31
  • 4