Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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}}

like image 893
Yaroslav Osetrov Avatar asked Jul 29 '13 13:07

Yaroslav Osetrov


People also ask

How do you convert Epoch to human readable date?

Convert from epoch to human-readable dateString date = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(new java.util.Date (epoch*1000)); Epoch in seconds, remove '*1000' for milliseconds. myString := DateTimeToStr(UnixToDateTime(Epoch)); Where Epoch is a signed integer. Replace 1526357743 with epoch.

How long are Unix timestamps?

The Unix time_t data type that represents a point in time is, on many platforms, a signed integer, traditionally of 32 bits (but see below), directly encoding the Unix time number as described in the preceding section. Being 32 bits means that it covers a range of about 136 years in total.


2 Answers

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.

like image 74
Sergei Basharov Avatar answered Oct 29 '22 12:10

Sergei Basharov


Use format date filter like this:

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

Reference

like image 27
EpokK Avatar answered Oct 29 '22 12:10

EpokK