admin管理员组

文章数量:1321447

In one of the App I am getting date as following formate. Mon Aug 18 23:59:59 IST 2014 Below is ng-repeat for angular js

Offer ends:  {{msg.endTime}}

Which render as app receive date in this format. Offer ends: Mon Aug 18 23:59:59 IST 2014

I want to show remaining time instead of Date/Time. Like Offers ends in 2days 40 minutes 10 seconds

Which is simple way to achieve with and without worry about TIMEZone

I am planning to use momentjs with format moment().format("ddd MMM HH:mm:SS")

Demo app /

In one of the App I am getting date as following formate. Mon Aug 18 23:59:59 IST 2014 Below is ng-repeat for angular js

Offer ends:  {{msg.endTime}}

Which render as app receive date in this format. Offer ends: Mon Aug 18 23:59:59 IST 2014

I want to show remaining time instead of Date/Time. Like Offers ends in 2days 40 minutes 10 seconds

Which is simple way to achieve with and without worry about TIMEZone

I am planning to use momentjs with format moment().format("ddd MMM HH:mm:SS")

Demo app https://flipkartoffers.firebaseapp./

Share Improve this question edited Aug 26, 2014 at 3:13 Kato 40.6k6 gold badges122 silver badges149 bronze badges asked Aug 25, 2014 at 19:38 Wasim ShaikhWasim Shaikh 7,04018 gold badges62 silver badges88 bronze badges 2
  • You should try moment's calendar() method, which automagically does the "in 2 days" and so on. – Kato Commented Aug 26, 2014 at 3:12
  • Have you checked the answers we provided? If one of them fits, please accept it or share your own solution if you found one. Thanks in advance. – apairet Commented Aug 28, 2014 at 15:31
Add a ment  | 

3 Answers 3

Reset to default 5

you can create your own filter for that pourpose ...

yourOwnApp.filter('datetimeToNow', function() {
  return function(input) {
    if(!input) return;

    return moment(input).fromNow();
  };
});

... subsequently just call it from within your ngRepeat:

Offer ends:  {{msg.endTime | datetimeToNow}}

Cheers, Luca

It is very easy to achieve using moment and angular-moment. You can use the am-time-ago directive. https://github./urish/angular-moment

<span am-time-ago="message.time"></span>
<span am-time-ago="message.time" am-preprocess="unix"></span>

am-time-ago works for both 'negative' time (past) and 'positive' time (future)

I fixed using Luca's answer , and changed formate of the date as below. Removed the IST Mon Aug 18 23:59:59 2014

<span>Ends {{msg.endTime | formatTime}}</span>

-

myApp.filter('formatTime', function() {
          return function(text) {
            var m = moment(text , 'ddd MMM DD HH:mm:ss YYYY');
            return m.fromNow();
        }
      })

本文标签: javascriptConvert date to remaining time using momentjsStack Overflow