Moment.js amDateFormat always returning date from 1970
Solution 1
I'm just quickly summarizing the problem and solution.
Moment.js offers two different ways to create a date from a unix timestamp
moment(1432252800)
and moment.unix(1432252800)
.
Both start at the same time (Jan 1 1970 12AM UTC) but moment()
uses the number as milliseconds, which are around 17 days and moment.unix()
uses seconds.
angular-moment supports the amFromUnix
filter, see source
You can use it the following way
<time am-time-ago="myDate|amFromUnix">
{{myDate|amFromUnix|amCalendar}}
Solution 2
Try to write own filter, like this:
newapp.filter("fromTimestamp", function(){
return function(timestamp, format){
return moment.unix(timestamp).format(format)
}
})
And use them
<p class="date">{{date | fromTimestamp:'dddd, MMMM Do YYYY'}}</p>
Leon Gaban
Investor, Powerlifter, Crypto investor and global citizen You can also find me here: @leongaban | github | panga.ventures
Updated on June 27, 2022Comments
-
Leon Gaban almost 2 years
http://plnkr.co/edit/5zxXEEz30t51yGhgYWVF?p=preview
I'm using Moment.js and Angular-moment in my app.
For some reason it's converting all my epoch timestamps to the same date from 1970.
<td class="timespan">{{tag.added_epoch | amDateFormat:'dddd, MMMM Do YYYY'}}</td>
This is what the
tag.added_epoch
value isadded_epoch: 1432252800
However when I convert it online, I get the correct date:
Any idea why my filter is turning
1432252800
into Saturday, January 17th 1970? -
Leon Gaban about 8 yearsThanks! +1 that is super clean :) Arminmsg however did answer my exact question, but your solution may actually be used to replace our code.
-
Enver Dzhaparoff about 8 years@LeonGaban Thanks for +1)