I'm currently working on a piece where I'm required to calculate the difference in time, that is for e.g an event has occurred on 22 Jan and today is 27th Jan, so I want to compare the two dates and display "5 days ago". I'm getting the event date as a json data in timestamp format. Any clue in how to compare it with todays date and display the 'days ago' logic
Asked
Active
Viewed 1,275 times
0
-
2Possible duplicate of [How to calculate the number of days between two dates using JavaScript?](http://stackoverflow.com/questions/2627473/how-to-calculate-the-number-of-days-between-two-dates-using-javascript) – Rayon Jan 27 '16 at 07:48
-
Please provide code where you attempted this – unicorn2 Jan 27 '16 at 07:49
-
Check this post: http://stackoverflow.com/questions/24883308/convert-birthday-to-age-in-angularjs/24883386?noredirect=1#comment57777768_24883386 – Michael Kang Jan 27 '16 at 08:08
5 Answers
0
You can use momentjs and it's diff function.
Usage:
moment().diff(Moment|String|Number|Date|Array);
Example:
var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
var difference = a.diff(b, 'days')
Just include the script in your index.html (or however else you include them), then include moment
in your controller using the regular dependency injection.

Noah
- 4,601
- 9
- 39
- 52
0
var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
var today = new Date();
var secondDate = new Date(2016,01,27);
var diffDays = Math.round(Math.abs((today.getTime() - secondDate.getTime())/(oneDay)));

Charlie
- 22,886
- 11
- 59
- 90
0
You can do something like to calculate date difference:
function DateDiff () {
return {
diff: diff,
};
function diff (date1, date2) {
return dateDiff(date1 - date2);
}
function dateDiff (ms) {
var d, h, m, s, data;
data = {};
s = Math.floor(ms / 1000);
m = Math.floor(s / 60);
s = s % 60;
h = Math.floor(m / 60);
m = m % 60;
d = Math.floor(h / 24);
h = h % 24;
data.days = d;
data.hours = h;
data.minutes = m;
data.seconds = s;
return data;
}
}
console.log(DateDiff.diff(Date1_object, Date2_object));
Based on the values returned by diff, you can display your text. You'll difference in days, hours, minutes and second and this information should be good enough to display the text you want to display.

Ravish
- 2,383
- 4
- 37
- 55
0
Use this :
<input id="first" value="8/5/2012" placeholder="m/d/yyyy"/>
<input id="second" value="10/15/2012"/><hr>
<button id="btn">Show Days</button>
<div id="result"></div>
<hr>
JQuery :
$("#btn").click( function(){
$("#result").text("Number of days : " + daydiff(parseDate($('#first').val()), parseDate($('#second').val())));
});
function parseDate(str) {
var mdy = str.split('/')
return new Date(mdy[2], mdy[0]-1, mdy[1]);
}
function daydiff(first, second) {
return (second-first)/(1000*60*60*24)
}
$("#btn1").click( function(){
alert(monthDiff(new Date(2011, 0, 1), new Date()));
var a = $('#first').val();
var b = $('#second').val();
});
Result :

Harshad
- 1,344
- 1
- 10
- 25
0
If you want to compare with todays date use angular-moment's am-time-ago
, and add it to html like this:
<span am-time-ago="time"></span>
Make sure time
is defined in the controller as a date/moment object

Aman Gupta
- 3,627
- 4
- 40
- 64