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

All we need is an easy explanation of the problem, so here it is.

I am getting data from a database and displaying it:

       <li ng-repeat="item in items>

Where {{}} 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 –{{}}

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

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>{{ * 1000 | date:'yyyy-MM-dd HH:mm:ss Z'}}</mydate>

Otherwise your date will be wrong.

Method 2

Use format date filter like this:

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


Method 3

If you have a Unix timestamp, you’ll probably have to multiply your timestamp by 1000 since the Unix timestamp is in seconds and AngularJs date filter needs milliseconds.

vm.milliseconds = Date('1441981121' * 1000);

then use $filter() function

var date = $filter('date')(vm.milliseconds, 'd MMMM yyyy');

or you can use in ng-bind

<span ng-bind="myController.milliseconds | date : 'd MMMM yyyy'"></span>

Method 4

You should use the date filter that is already provided by angular:

Method 5

 yourapp.filter('timestampToDate', function () {
    return function (timestamp) {
        var date = new Date(timestamp * 1000);
        var dateObject = date.getFullYear() +'/'+ ('0' + (date.getMonth() + 1)).slice(-2) +'/'+ ('0' + date.getDate()).slice(-2);
        return dateObject;

{{timestamp | timestampToDate}}

Method 6

There is directive to that called rsTime.

    <rs-time start-time="1478513323" digital-format="'ddd MMM d h:mm TT'"></rs-time>

Check demo at Plunker .

Below are different formates of time:

 M/d/y -  11/7/2016

 HH:mm:ss - 16:02:31 (24hrs formate)

 hh:mm:ss TT - 04:03:10 PM

 ddd MMM d h:mm TT  - Mon Nov 7 at 4:04 PM

For documentation visit Github

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply