简体   繁体   English

日期格式过滤器不起作用角度js

[英]Date format filter is not working angular js

I am new to angular js . 我是angular js的新手。 I am trying to give some date format like Nov-24-2016 , but its not working at all . 我正在尝试提供一些日期格式,例如Nov-24-2016 ,但它根本无法正常工作。 Below is my code. 下面是我的代码。

{{ x.created | date:" MM-d-y" }}

Here I want this format Nov-24-2016 在这里我想要这种格式Nov-24-2016

and x.created value = 2016-11-24 08:02:21 x.created value = 2016-11-24 08:02:21

Any suggestion? 有什么建议吗? Thank you 谢谢

you can create a custom 您可以创建一个自定义

.filter('datetime', function($filter){
    return function(input){
      if(input == null){ return ""; } 

      var _date = $filter('date')(new Date(input),'MM-dd-yyyy');         
      return _date.toUpperCase();

    };
});


{{ x.created | datetime }}

Have a look at the documentation: https://docs.angularjs.org/api/ng/filter/date 看一下文档: https : //docs.angularjs.org/api/ng/filter/date

x.created must be either a Date object or a timestring with milliseconds since UTC epoch. x.created必须是Date对象或自UTC时代以来以毫秒为单位的时间字符串。

So either create a manual filter for it or parse your datestring to a Date object. 因此,要么为其创建一个手动过滤器,要么将您的日期字符串解析为一个Date对象。

使用datex.created必须是日期对象,数字(因为UTC毫秒为单位)或ISO字符串作为每DatePipe

{{ x.created | date: "MMM-dy" }} {{ x.created | date: "MMM-dy" }} renders to Nov-24-2016 {{ x.created | date: "MMM-dy" }}渲染至Nov-24-2016

Btw: 2016-11-24 08:02:21 seems not to be a valid ISO string, https://www.w3.org/TR/NOTE-datetime . 顺便说一句: 2016-11-24 08:02:21似乎不是有效的ISO字符串, https : 2016-11-24 08:02:21 You could check this. 您可以检查一下。

I had similar problem. 我有类似的问题。 It's fix after 2 steps, change document encoding to utf-8 without BOM and rewrite manually data of time. 经过两步修复,将文档编码更改为不带BOM的utf-8,并手动重写了时间数据。 Like this - date: 2013-12-02T17:57:28.556094Z 像这样-日期: 2013-12-02T17:57:28.556094Z

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

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