Khalid
Khalid

Reputation: 467

how can i set time in angularjs as [ 2 min ago ] if [difference is 2 min from current time]?

I am trying to set a time as for example , i have time as [ 01:25:00 ] then if now current time is [ 01:28:00 ] . It should show as "3 min ago". how can i do using angular and ionic.

i am trying some thing like :

1 min ago , 2 min ago , 30 min ago , 1 hour ago

my html code:

      <div class="item item-divider item-input-wrapper">
             <p>Time : {{ messages.MSGTIME | date:'shortTime'}} </p>
      </div> 

Thanks in advance

Upvotes: 1

Views: 1507

Answers (3)

hansmaad
hansmaad

Reputation: 18915

If you don't want to use moment.js (what I would recommend) you can write your own date filter. A simple example (http://plnkr.co/edit/BVH5Q3?p=preview):

angular.module('app').filter('myDate', myDate);

function myDate() {
    var s = 1000;
    var m = s * 60;
    var h = m * 60;
    return function(date) {
        var d = date.getTime() - Date.now();
        if (d > h)
            return '' + d / h + ' hours ago';
        if (d > m)
            return '' + d / m + ' minutes ago';
        if (d > s)
            return '' + d / s + ' seconds ago';
        return '' + d + ' ms ago';;
    }
}

Upvotes: 2

j2L4e
j2L4e

Reputation: 7060

I'm in a hurry right now, but here something from the top of my head for a q'n'd approach:

  • Create own filter
  • split(":") both time strings
  • calc difference of hours and minutes
  • then:

    ret = (diffHours > 0)? diffHours + " hours and " : "";
    ret += diffMinutes + " minutes ago";
    return ret;
    

Upvotes: 0

Mudasser Ajaz
Mudasser Ajaz

Reputation: 6257

Use angular-moment , filter amDifference can fullfill your purpose. You cna use it like this :

 <p>Time : {{ messages.MSGTIME | amDifference : null : 'hours'}} </p>

Upvotes: 0

Related Questions