Reputation: 33
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.
Upvotes: 0
Views: 21038
Reputation: 305
$('#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>
For more info - moment js - two dates difference in number of days
Upvotes: 4
Reputation: 1
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'));
Upvotes: 0
Reputation: 470
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.
Upvotes: 0