代码之家  ›  专栏  ›  技术社区  ›  Hemadri Dasari

如何将物料日期选择器日期值格式化为6中的“MM-DD-YYY”格式?

  •  2
  • Hemadri Dasari  · 技术社区  · 7 年前

    我正在尝试将以下日期格式转换为以下格式MM-DD-YYYY在Angular 6中。我在SO和其他网站上也看到了一些更简单的问题,但我无法解决它。

    Angular DatePicker 组件。

    日期选择器给我的

    Thu Oct 25 2018 00:00:00 GMT+0400 (Gulf Standard Time)
    

    我想把它格式化成

    MM-DD-YYYY
    

    我的代码

    <mat-form-field>
        <input matInput [min]="minDate" [matDatepicker]="date" [(ngModel)]="request.date | request.date: 'dd/MM/yyyy hh:mm a'" placeholder="Choose a date">
        <mat-datepicker-toggle matSuffix [for]="date"></mat-datepicker-toggle>
        <mat-datepicker #date></mat-datepicker>
    </mat-form-field>
    

    我也尝试过像下面这样的DatePipe模块,但没有任何效果

    this.datePipe.transform(this.request.date,"MM-DD-YYYY")
    
    2 回复  |  直到 7 年前
        1
  •  6
  •   Prashant Pimpale Dila Gurung    7 年前

    你可以用 DatePipe

    导入main.module.ts

    import { DatePipe } from '@angular/common';
    

    Datapipe 在提供程序数组中。

    在构造函数中

    constructor(private datePipe : DatePipe)
    {
    
    }
    

    this.datePipe.transform(your_date, 'MM-dd-yyyy');
    

    看一看 StackBlitz Example MM-dd-yyyy

        2
  •  1
  •   Prashant Pimpale Dila Gurung    7 年前

    使用 dateformat 从角公共函数。

    参考 this 回答

        3
  •  1
  •   Leandro Q    7 年前

    在使用mat datepicker的组件中尝试这个

    从“@angular/material”导入{DateAdapter};

    this.dateAdapter.setLocale('your locale');}

        4
  •  0
  •   Narendra Singh Rathore    5 年前
    import { formatDate } from '@angular/common';
    
    formatDate(date, 'MMM, yyyy', 'en-US');
    

    角度9

    推荐文章