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

查看:31
本文介绍了使用 momentjs 更改 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.

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

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