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}}
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.
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.
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.
Use format date filter
like this:
<mydate>{{item.date * 1000 | date:'yyyy-MM-dd HH:mm:ss Z'}}</mydate>
Reference
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With