Moment.js amDateFormat always returning date from 1970

半城伤御伤魂 提交于 2019-12-01 05:34:58

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

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>

Plunker demo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!