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

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

问题描述

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

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

我在mat-date picker中尝试过format标记,但是它不起作用,其他日期选择器(例如ngbDatepicker format)可以通过一行编码轻松更改

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>

从日期选择器中选择后显示的日期应为DD-MM-YYYY,从日期选择器中检索值时,其日期也应为DD-MM-YYYY格式

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天全站免登陆