简体   繁体   English

时间戳在AngularJS插值中转换为错误的日期

[英]Timestamp is converted to wrong date inside AngularJS' interpolation

Having this timestamp: 1519347000 , I want to convert it into date format inside interpolation like this: 具有以下时间戳记: 1519347000 ,我想将其转换为插值内的日期格式,如下所示:

{{$ctrl.myTimestamp | date:'MMM d y, hh:mm'}}

The resulted values is Jan 18 1970, 04:02 which is obviously wrong, the correct one should be in this case February 23, 2018 12:50 结果值是1970年1月18日,04:02这显然是错误的,在这种情况下正确的值应该是2018年2月23日12:50

Plunker here . 在这里插一下

Any ideas what's going wrong? 任何想法出什么事了吗?

You currently have the seconds till Unix epoch . 您目前距离Unix时代只有几秒钟的路程

The date filter from angular requires the milliseconds. 角度的日期过滤器需要毫秒。 If you multiply your timestamp by a thousand you get the milliseconds till Unix Epoch. 如果您将时间戳乘以一千,您将获得直到Unix Epoch的毫秒数。

The easiest fix is as follows: 最简单的解决方法如下:

{{($ctrl.myTimestamp * 1000) | date:'MMM d y, hh:mm'}}

您需要将传递的值乘以1000然后才能获得正确的日期

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM