最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Moment.js amDateFormat always returning date from 1970 - Stack Overflow

programmeradmin1浏览0评论

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 is added_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?

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 is added_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?

Share Improve this question edited Jan 29, 2016 at 20:41 Leon Gaban asked Jan 29, 2016 at 20:21 Leon GabanLeon Gaban 39k122 gold badges348 silver badges549 bronze badges 6
  • 1 Have you tried moment.unix() in angular-moment it would be <span am-time-ago="message.unixTime | amFromUnix"> – Arminmsg Commented Jan 29, 2016 at 20:28
  • 1 I get this error Unknown provider: amFromUnixFilterProvider <- amFromUnixFilter looks like I need to use a different version of the lib :( but are my numbers correct? – Leon Gaban Commented Jan 29, 2016 at 20:33
  • 1 I've took a quick look at moment.js and I get 1970 if I just use moment(1432252800) I get the 1970 date with the unix method I get the 2015 date, so your numbers seem to be correct. From the angular-moment docs: Note: To use amFromUnix, install angular-moment version 1.0.0-beta.3 – Arminmsg Commented Jan 29, 2016 at 20:36
  • @Arminmsg how? They don't have that build up :( github.com/urish/angular-moment/releases oh nvm found it on the CDN cdnjs.cloudflare.com/ajax/libs/angular-moment/1.0.0-beta.3/… – Leon Gaban Commented Jan 29, 2016 at 20:42
  • 1 The latest build is the 1.0.0-beta3, take a look at the label – Arminmsg Commented Jan 29, 2016 at 20:47
 |  Show 1 more comment

2 Answers 2

Reset to default 26

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

发布评论

评论列表(0)

  1. 暂无评论