简体   繁体   中英

How to format date from date picker angular material in format ('Y-MM-DD')? Reactive form

I have tried everything but I am not succeeding. In the example where I do not do the reactive form but the ng-model, it went very easy for me:

deliveryDate: moment(this.startDate).format('Y-MM-DD')

But in current code in Angular form I Can't change anything. Look my code:

   export const MY_FORMATS = {
  parse: {
    dateInput: 'MM/DD',
  },
  display: {
    dateInput: 'MM/DD',
    monthYearLabel: 'MMM YYYY',
    dateA11yLabel: 'LL',
    monthYearA11yLabel: 'MMMM YYYY',
  },
};


@Component({
  selector: 'app-my-order',
  templateUrl: './my-order.component.html',
  styleUrls: ['./my-order.component.scss'],
  providers: [
    // `MomentDateAdapter` can be automatically provided by importing `MomentDateModule` in your
    // application's root module. We provide it at the component level here, due to limitations of
    // our example generation script.
    {
      provide: DateAdapter,
      useClass: MomentDateAdapter,
      deps: [MAT_DATE_LOCALE, MAT_MOMENT_DATE_ADAPTER_OPTIONS]
    },

    {provide: MAT_DATE_FORMATS, useValue: MY_FORMATS},
  ],
})
 resultOfdate: any = MY_FORMATS;


this.getProducts();
this.form = this.fb.group({
  deliveryDate: [moment(this.resultOfdate).format('2020-MM-DD'), Validators.required],  //this is not work format is hard-code on 2020 because deliveryDate from form return current date like a new Date(). this.resultOfdata is
  address: [null, [Validators.required, Validators.minLength(5)]],
  phone: [null, [Validators.minLength(9),Validators.maxLength(19)]],
  city: [null, [Validators.required, Validators.minLength(5)]],
  data: this.fb.array([this.createContact()]),
  note: [null]
});

And my template:

        <mat-form-field>
            <input formControlName="resultOfdate"  matInput [matDatepicker]="resultOfdate" placeholder="Choose a date" >
            <mat-datepicker-toggle matSuffix [for]="resultOfdate" ></mat-datepicker-toggle>
            <mat-datepicker #resultOfdate></mat-datepicker>
          </mat-form-field>

I want to send like a 2019-10-10 example

You are probably using the moment.format in the wrong place.

try it like this, calling your method on dateChange like this:

export const MY_FORMATS_ORG = {
  parse: {
    dateInput: 'MM/DD',
  },
  display: {
    dateInput: 'MM/DD',
    monthYearLabel: 'MMM YYYY',
    dateA11yLabel: 'LL',
    monthYearA11yLabel: 'MMMM YYYY',
  },
};

@Component({
  selector: 'datepicker-formats-example',
  templateUrl: 'datepicker-formats-example.component.html',
  styleUrls: ['datepicker-formats-example.component.css'],
  providers: [
    {provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE]},
    {provide: MAT_DATE_FORMATS, useValue: MY_FORMATS_ORG},
  ],
})
export class DatepickerFormatsExampleComponent {
  date = new FormControl(moment());
  dateData: string;

  public setDate(date: string) {
    this.dateData = date ? moment(date).format('Y-MM-DD') : '';
  }
}

Check it here: https://stackblitz.com/edit/angular-material-datepicker-6hjuhq?file=src/app/app.component.ts

Also, this:

deliveryDate: moment(this.startDate).format('Y-MM-DD')

would be wrong, should be something like:

deliveryDate: string;

and the assignment will be done in the method.

I hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM