Angular Material DatePicker: Month and Day, Exclude Year

后端 未结 2 517
南笙
南笙 2020-12-21 23:52

How do I create a Month and Day Date Picker in Angular, excluding hide year?

This following link will do a Month and Year picker. I am trying to manipulate it, to do

相关标签:
2条回答
  • 2020-12-22 00:09

    I hope you are expecting date format like DD/MMM. If so then change dateInput in display and parse object like below

    dateInput: 'DD/MMM'
    

    Hope this helps.

    Here is the stackblitz code.

    https://stackblitz.com/edit/angular-hw54xf

    0 讨论(0)
  • 2020-12-22 00:14

    image

    So , first in the html file

     <mat-form-field class="mat-50-left" (click)="updateCalendarUI()">
        <input matInput [matDatepicker]="picker_start"
               placeholder="Date de début" required formControlName="dt_start" (click)="picker_start.open();">
        <mat-datepicker-toggle matSuffix (click)="picker_end.open(); updateCalendarUI()"></mat-datepicker-toggle>
        <mat-datepicker #picker_start startView="year"></mat-datepicker>
      </mat-form-field>
    

    in the .ts file

           import {DateAdapter, NativeDateAdapter} from "@angular/material/core";
           import * as moment from "moment";
    
        export class AppDateAdapter extends NativeDateAdapter {
           format(date: Date, displayFormat: Object): string {
            // use what format you need
            return moment(date).format('DD MMM');
          }
        }
    

    add in providers

      providers: [{provide: DateAdapter, useClass: AppDateAdapter}]
    

    To update calendar UI

      updateCalendarUI(): void {
    
         setTimeout(() => {
           let calendar = document.getElementsByClassName('mat- 
          calendar').item(0);
          if (calendar) {
             calendar['style'].height = '275px';
             calendar['style']['padding-top'] = '15px';
            }
         let header = document.getElementsByClassName('mat-calendar- 
          header').item(0);
          if (header) {
             header['style'].display = 'none';
          }
          let yearLabel = document.getElementsByClassName('mat-calendar-body- 
           label').item(0);
          if (yearLabel) {
              yearLabel['style'].display = 'none';
          }
        }, 1);
       }
    
    0 讨论(0)
提交回复
热议问题