Difference between two dates moment.js

I have a formatted date that comes as a string "19/04/2018" I need to compare it with the date today and know how many days have passed.

I'm trying it this way but it does not work properly.

var stringDate =  $scope.dataAddOutpatient.date; //Return "19/04/2018" 
var today = moment().format("DD/MM/YYYY");
var date = moment(stringDate);

//Difference in number of days
var dates = moment.duration(date.diff(today)).asDays();

Always return 0. What's wrong?

In the examples that I have seen compare objects of type string and I need to compare it with an object of type moment.

$('#test').click(function() {
    var startDate = moment("13/04/2016", "DD/MM/YYYY");
	var currenDate = moment(new Date()).format("DD/MM/YYYY");
	var endDate = moment(currenDate, "DD/MM/YYYY");
    var result = 'Diff: ' + endDate.diff(startDate, 'days');
    $('#result').html(result);
});
#test {
  width: 100px;
  height: 100px;
  background: #ffb;
  padding: 10px;
  border: 2px solid #999;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.12.0/moment.js"></script>

<div id='test'>Click Me!!!</div>
<div id='result'></div>

Difference, You were close. You just need to use the duration.asHours() method (see the docs). var duration = moment.duration(end.diff(startTime)); var hours = duration. Moment.js offers a way to calculate the difference between two dates. The difference is calculated in milliseconds by default, but can also be returned in days, months, years, etc. To compute the

Based on the the moment.js documentation, Let's check what they suggest:

var dateA = moment([2007, 0, 29]);
var dateB = moment([2007, 0, 28]);
dateA.diff(dateB, 'days'); // result is 1

Try this way, might this resolved your issue.

Get hours difference between two dates in Moment Js, Date difference with Moment objects. You can also create Moment objects and then calculate the difference: var admission = moment('{date1}', 'DD-MM-YYYY');​  Moment.js – Get difference in two dates in years, months and days. You could get the difference in years and add that to the initial date; then get the difference in months and add that to the initial date again. In doing so, you can now easily get the difference in days and avoid using the modulo operator as well.

var toDay= moment();
var startDate = moment('2017-11-09');
console.log('Milliseconds:',toDay.diff(startDate));
console.log('days:', toDay.diff(startDate, 'days'), 'days');
console.log('months:', toDay.diff(startDate, 'months'));

To find the difference between two dates using moment.js, You can use moment.js, which is a fully-featured date library for parsing and manipulating dates and times. You would declare your two dates as moment objects  When you call diff, moment.js calculates the difference in milliseconds. If the milliseconds is passed to duration, it is used to calculate duration which is correct.

Docs, The idea is to have 2 custom date fields and use moment.js to determine the difference between them in a format similar to 'x Years, x Months and  Try this Using moment.js (Its quite easy to compute date operations in javascript) firstDate.diff(secondDate, 'days', false); // true|false for fraction value Result will give you number of days in integer.

Calculate the difference between two dates, In this post, we will see how to find the difference between two dates in days with Moment.js is a lightweight JavaScript date library for parsing, validating,  Instead of modifying the native Date.prototype, Moment.js creates a wrapper for the Date object. To get this wrapper object, simply call moment() with one of the supported input types. The Moment prototype is exposed through moment.fn. If you want to add your own functions, that is where you would put them.

How to calculate the time difference between two dates in hours , Get hours difference between two dates in Moment Js - Wikitechy. var duration = moment.duration(end.diff(startTime)); var hours = duration. To get the duration of a difference between two moments, you can pass diff as an argument into moment#duration. See the docs on moment#duration for more info. The supported measurements are years, months, weeks, days, hours, minutes, and seconds. For ease of development, the singular forms are supported as of 2.0.0.

Comments
  • Possible duplicate of moment js - two dates difference in number of days
  • In this example, compare two objects of type string and I need to compare them with a moment type object. The current date will provide me with a moment object.
  • @Bibi, we can get current date as a string var currenDate = moment(new Date()).format("DD/MM/YYYY");
  • Uncaught TypeError: currenDate.diff is not a function
  • Great, thank you very much for your help!!
  • var toDay= moment(); var startDate = moment(stringDate); console.log('Milliseconds:',toDay.diff(startDate)); console.log('days:', toDay.diff(startDate, 'days'), 'days'); console.log('months:', toDay.diff(startDate, 'months'));
  • Milliseconds: NaN days: NaN days months: NaN
  • Check with "stringDate" variable. var stringDate='2017-09-23'; then use above diff method.