首页 文章

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

提问于
浏览
3

如何以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)

1 回答

  • 3

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

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

    DEMO

相关问题