如何使用 angular ui bootstrap 设置默认格式日期 [英] How to set default format date with angular ui bootstrap

查看:36
本文介绍了如何使用 angular ui bootstrap 设置默认格式日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ui.boostrap 作为日期选择器,

I use ui.boostrap for a datepicker,

http://plnkr.co/edit/GfOQmgW85U2aW3YbZO7T?p=preview

我需要将日期格式化为yyyy/MM/dd",因为这就是我的 RESTapi 接收参数的方式.以角度应用 $filter.

I need to format the date like "yyyy/MM/dd" because that's how my RESTapi receives the args. Applying $filter in angular.

http://docs.angularjs.org/api/ng.filter:date

问题似乎解决了,但是当我在日期选择器中更改日期时,格式日期更改为类似 2014-01-30T00:58:43.000Z

It seems that the problem was solved, however when I change the date in the datepicker, the format date changes to a format like 2014-01-30T00:58:43.000Z

如何使用此工具设置默认格式日期?

how can I set default format date with this tool?

推荐答案

由于日期是一个 JS Date 对象,所以您必须在发送之前对其进行转换.发送前可以使用 datefilter 手动解析:

Since the date is a JS Date object, you'll have to convert it before sending. You can use the datefilter to parse manually before sending:

var datefilter = $filter('date'),
    formattedDate = datefilter($scope.dt, 'yyyy/MM/dd');

查看此 plunk 示例:http://plnkr.co/edit/EJLDpEojoFnf5QfFt4o6?p=preview

See this plunk for an example: http://plnkr.co/edit/EJLDpEojoFnf5QfFt4o6?p=preview

我所知道的唯一替代方法是为该值创建一个指令并将函数推送到 $parsers,例如 这个例子,但这绝对不容易与 datepicker 指令结合使用.

Only alternative I know of, is creating a directive for the value and pushing a function to $parsers like in this example, but that's definitely not easy to combine with the datepicker directive.

我建议继续在 JS 中使用 Date 对象,并在发送到您的 API 之前转换该值.

I'd suggest to continue using the Date object in JS, and just convert the value before sending to your API.

这篇关于如何使用 angular ui bootstrap 设置默认格式日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆