0

是否可以在 ngb-datepicker startDate 中使用 YYYY-MM-DD ?

现在我得到了

<input class="form-control ml-2" placeholder="{{awayFrom}}" [startDate]="{year: 2018, month: 11, day: 11}"  name="awayFrom" [(ngModel)]="awayFrom" ngbDatepicker (click)="d.toggle()" #d="ngbDatepicker">

但是我要:

<input class="form-control ml-2" placeholder="{{awayFrom}}" [startDate]="startDate"  name="awayFrom" [(ngModel)]="awayFrom" ngbDatepicker (click)="d.toggle()" #d="ngbDatepicker">

其中 startDate 是从 Api 获取的日期。这个日期的格式是 YYYY-MM-DD

4

1 回答 1

0

DatePicker accepts date only in object format. So what you need to do is convert your string date to an object. You can do it by creating a Pipe and DatePicker Date Type

@Pipe({name: 'datePickerDatePipe'})
export class DatePickerDatePipe implements PipeTransform {
  transform(api_date_string: string): DatePickerDate {
    let date =  new Date(api_date_string);
    let calendarDate = {year: date.getFullYear(), month: date.getMonth(), day: date.getDay()};
    return calendarDate;
  }
}

export type DatePickerDate = {year: number, month: number, day: number};

// usage in HTML:
{{model.startdate | datePickerDatePipe}} 
于 2020-06-17T11:28:20.513 回答