How can I calculate the elapsed time between two date objects in ionic?
I have a date object created from vars stored in a database.
var prevdate = yyyy-mm-dd hh:mm:ss;
I want to calculate the current time (date now ()) and want to show a list in my Ionic app like my concept is below like "2 days ago" or "One moment ago" or "One hour ago". How can I achieve this?
+3
source to share
3 answers
Better to use http://momentjs.com/ to handle dates with js. It provides most of the functionality we need related to date. You can get the difference of dates at a moment using
moment('2015-06-24 19:57:00', "YYYYMMDD").fromNow();
0
source to share
try it
var past=new Date('2015-06-24 19:57:00');
var now= new Date();
var diff=msToTime(now-past);
console.log(diff.toString());
function msToTime(s) {
var ms = s % 1000;
s = (s - ms) / 1000;
var secs = s % 60;
s = (s - secs) / 60;
var mins = s % 60;
var hrs = (s - mins) / 60;
if(hrs==0 && mins==0)
return 'just a moment ago';
else if(hrs==0)
return mins+' mins ago';
else if(hrs<24)
return hrs+' hours ago';
else
return Math.floor(hrs/24)+' days ago';
}
Try changing the old var to see different results.
+2
source to share
here is an example of a delta time. Time difference in Nodejs?
then create a function like the previous answer, prompting you to decide on what to return as a string.
+1
source to share