Moment.js amDateFormat 总是返回 1970 年的日期

Moment.js amDateFormat always returning date from 1970(Moment.js amDateFormat 总是返回 1970 年的日期)
本文介绍了Moment.js amDateFormat 总是返回 1970 年的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

<td class="timespan">{{tag. added_epoch |amDateFormat:'dddd, MMMM Do YYYY'}}</td>

这就是 tag.added_epoch 值是 added_epoch: 1432252800

但是当我在线转换时,我得到了正确的日期:

知道为什么我的过滤器会将 1432252800 变成 Saturday, January 17th 1970?

解决方案

我只是快速总结问题和解决方案.

Moment.js 提供了两种不同的方法来根据 unix 时间戳创建日期moment(1432252800)moment.unix(1432252800).

两者同时开始(1970 年 1 月 1 日凌晨 12 点 UTC),但 moment() 使用以毫秒为单位的数字,大约是 17 天和 moment.unix() 使用秒数.

angular-moment 支持 amFromUnix 过滤器,参见 来源

你可以通过以下方式使用它

<time am-time-ago="myDate|amFromUnix">{{myDate|amFromUnix|amCalendar}}

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?

解决方案

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

这篇关于Moment.js amDateFormat 总是返回 1970 年的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
Ordinals in words javascript(javascript中的序数)
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)