如何以最简单的方式将 Mat-Datepicker 日期格式更改为 DD/MM/YYYY? [英] How to change Mat-Datepicker date format to DD/MM/YYYY in simplest way?

查看:39
本文介绍了如何以最简单的方式将 Mat-Datepicker 日期格式更改为 DD/MM/YYYY?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 DOB 设置一个 mat-datepicker,传统上显示格式是 MM/DD/YYYY,我需要将其更改为 DD/MM/YYYY,编码较少

我在 mat-date 选择器中尝试过格式化标签,但它不起作用,其他日期选择器如 ngbDatepicker 格式可以通过一行代码轻松更改

 

<mat-form-field><input matInput [matDatepicker]="picker2" [max]="currentDate"placeholder="DOB(DD/MM/YYYY)" 需要 formControlName="dob"只读/><mat-datepicker-toggle matSuffix [for]="picker2"></mat-datepicker-切换><mat-datepicker #picker2></mat-datepicker></mat-form-field>

从 mat-date 选择器中选择后显示的日期应该是 DD-MM-YYYY 并且当从日期选择器中检索值时它也应该是 DD-MM-YYYY 格式

解决方案

最简单的方法是更改​​语言环境:

将以下内容添加到模块的提供者部分:

{ 提供:MAT_DATE_LOCALE,useValue:'en-GB' }

I'm setting up a mat-datepicker for DOB and traditionally the display format is MM/DD/YYYY,I need to change it to DD/MM/YYYY with less coding

I tried format tag in mat-date picker but it does not work,other date pickers like ngbDatepicker format can easily changed through one line of coding

  <div class="col-md-3 Dinline-block">
     <mat-form-field>
        <input matInput [matDatepicker]="picker2" [max]="currentDate" 
         placeholder="DOB(DD/MM/YYYY)" required formControlName="dob" 
          readonly />
        <mat-datepicker-toggle matSuffix [for]="picker2"></mat-datepicker- 
         toggle>
        <mat-datepicker #picker2></mat-datepicker>
     </mat-form-field>
  </div>

The date displayed after selecting from mat-date picker should be DD-MM-YYYY and when the retrieving the value from date picker it should also be in DD-MM-YYYY format

解决方案

The easiest way is to change the locale:

Add the following to the providers section of your module:

{ provide: MAT_DATE_LOCALE, useValue: 'en-GB' }

这篇关于如何以最简单的方式将 Mat-Datepicker 日期格式更改为 DD/MM/YYYY?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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