使用moment.js更改Angular Material中的md-datepicker格式 [英] Change format of md-datepicker in Angular Material with momentjs

查看:180
本文介绍了使用moment.js更改Angular Material中的md-datepicker格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Angular material引入了一个新的日期选择器组件,该组件位于此处.

Angular material introduced a new date picker component found here.

我希望此组件返回的日期的格式为 yyy-mm-dd ,但是我不确定如何完成.通过搜索,我发现可以使用$mdDateLocaleProvider,但是找不到使用它的示例.

I want the date returned by this component to be in the format yyy-mm-dd but I am not sure how is this done. By searching I found that $mdDateLocaleProvider can be used, but I could not find an example of using it.

有人可以给我看一个格式化md-datepicker返回的日期的有效示例吗?

Can someone show me a working example of formatting the date returned by md-datepicker?

推荐答案

$mdDateLocaleProvider 在Angular Material文档中.

There is a documentation for $mdDateLocaleProvider in the Angular Material docs.

angular.module('app').config(function($mdDateLocaleProvider) {
    $mdDateLocaleProvider.formatDate = function(date) {
       return moment(date).format('YYYY-MM-DD');
    };
});

如果您不使用moment.js,请使用formatDate中的代码替换您希望用来格式化日期的任何内容.

If you don't use moment.js, substitute the code inside formatDate for whatever you wish to use to format the date.

此处是基于Angular Material文档中示例的CodePen示例.

Here is a CodePen example based on the samples from Angular Material docs.

这篇关于使用moment.js更改Angular Material中的md-datepicker格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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