Angular Material 5 – 如何在mat-datepicker中自定义日期

前端之家收集整理的这篇文章主要介绍了Angular Material 5 – 如何在mat-datepicker中自定义日期前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何以MM / DD / YYYY格式自定义mat-datepicker日期?

我正在使用以下配置:

HTML

<mat-form-field>
  <input matInput [matDatepicker]="picker" placeholder="Choose a date">
  <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
  <mat-datepicker #picker></mat-datepicker>
</mat-form-field>

TS:

import {Component} from '@angular/core';

/** @title Basic datepicker */
@Component({
  selector: 'datepicker-overview-example',templateUrl: 'datepicker-overview-example.html',styleUrls: ['datepicker-overview-example.css'],})
export class DatepickerOverviewExample {}

当我打印表单值时,它给出了以下输出

Sun Dec 31 2017 00:00:00 GMT+0530 (IST)

我期待MM / DD / YYYY的格式.

我试过这个配置:https://material.angular.io/components/datepicker/overview#customizing-the-parse-and-display-formats

但它对我不起作用.我正在使用默认的MatNativeDateModule(不是时刻js)

解决方法

您可以使用Date Pipe,如下所示,

<input mdInput placeholder="Title" [ngModel]="mydate  | date:'MM-dd-yyyy'">

DEMO

猜你在找的Angularjs相关文章