typescript 日期不显示在日期选择器输入字段中

svdrlsy4  于 7个月前  发布在  TypeScript
关注(0)|答案(1)|浏览(131)

我们如何只显示选定的月份和年份的输入字段,我选择后的值不显示.选定的月份和年份应该显示.什么似乎是问题?谢谢


的数据
示例值:this.selectedMonthYear 2025年6月

代码

<mat-form-field>
                <input [(ngModel)]="selectedMonthYear" matInput [matDatepicker]="picker" placeholder="Select month" readonly>
                <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
                <mat-datepicker #picker startView="multi-year" (monthSelected)="closeStartDatePicker($event, picker)"></mat-datepicker>
              </mat-form-field>

 selectedMonthYear: string = "";

 closeStartDatePicker(normalizedMonth: Date, dp?: any) {
      // Format the selected month and year
      // this.selectedMonthYear = normalizedMonth
      this.selectedMonthYear = this.formatMonthYear(normalizedMonth);
      console.log(" this.selectedMonthYear" ,  this.selectedMonthYear)
      // console.log('this.selectedMonthYear' ,)
      // Close the datepicker
      dp.close();
  }

  private formatMonthYear(date: Date): string {
    const month = date.toLocaleString('en-us', { month: 'long' });
    const year = date.getFullYear();
    return `${month} ${year}`;

字符串

7fhtutme

7fhtutme1#

大多数日期类型的时间输入字段接受某种格式的日期,通常是年-月-日(Y-m-d):例如2023-10-28。
因此,当传递日期字符串到字段时,尝试更改格式,直到找到可识别的格式。据我所知,Y-m-d是常见的。

相关问题