AngularJS. Convert tag value (Unix time to human-readable time)

angularjsangularjs-directive

I am getting data from a database and displaying it:

    <ul>
       <li ng-repeat="item in items>
          <mydate>{{item.date}}</mydate>
        </li>
    </ul>

Where {{item.date}} is a Unix date such as 1374843600. How can I set the date format using AngularJS directives? Is it possible?

When I tried to do it, I was getting a value of tag mydate –{{item.date}}

Best Answer

I have faced the issue with unix time formatted as a number of seconds from the epoch start or as a number of milliseconds that is used in JavaScript. So strictly speaking, AngularJS doesn't convert Unix timestamp to Date, but a number with milliseconds, which is 1000 times larger, so first you will have to multiply your input number by 1000, like this:

<mydate>{{item.date * 1000 | date:'yyyy-MM-dd HH:mm:ss Z'}}</mydate>

Otherwise your date will be wrong.

Related Topic