简体   繁体   English

使用Angular JS过滤器转换日期失败

[英]Converting date using Angular JS filter fails

I am using angular js filter to format a date on the controller. 我正在使用angular js过滤器来格式化控制器上的日期。 A user selects a date from a uib-datepicker-popup . 用户从uib-datepicker-popup选择一个日期。 It is this date that I want formatted using ISO 8601 (yyyy-mm-dd) date format. 我想使用ISO 8601 (yyyy-mm-dd)日期格式来格式化该日期。 I then log my the date picked by the user and get something like this 然后,我记录用户选择的日期并得到类似的信息

Sat Aug 20 2016 00:00:00 GMT+0300 (EAT)

However I then use angular $filter like this 但是我然后像这样使用angular $ filter

$filter('date')(new Date(vm.my_date), 'yyyy-mm-dd');

and wind up with the date as 然后以日期作为结束

2016-00-20

Here is my the code on my controller 这是我控制器上的代码

        // Logs Sat Aug 20 2016 00:00:00 GMT+0300 (EAT)
        console.log(vm.my_date);

        var converted = $filter('date')(new Date(vm.my_date), 'yyyy-mm-dd');

        // Logs 2016-00-20
        console.log(converted);

What am I doing wrong? 我究竟做错了什么?

过滤器应类似于“ yyyy-MM-dd”,请检查此文档角度过滤器

When you write the mm in your filter the filter thinks that you mean minutes with that. 当您将mm写入过滤器时,过滤器会认为您的意思是minutes
For the filter to understand the m s need to be UPPERCASE so it understands that you mean month . 为了使筛选器能够理解,必须将m设为大写,以便理解您的意思是month
Changing the filter from: 从以下位置更改过滤器:

var converted = $filter('date')(new Date(vm.my_date), 'yyyy-mm-dd');

to

var converted = $filter('date')(new Date(vm.my_date), 'yyyy-MM-dd');

would fix your issue. 将解决您的问题。

Why don't you use the ui.bootstrap.dateparser? 为什么不使用ui.bootstrap.dateparser? No need to create a custom filter when there's already one built into ui.bootstrap.datepicker. ui.bootstrap.datepicker中已内置一个过滤器时,无需创建自定义过滤器。 Angular Directives for Bootstrap Bootstrap的角度指令

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

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