0

Is possible to use in ngb-datepicker startDate as YYYY-MM-DD ?

Now I got

<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">

But I want to:

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

where startDate is Date getting from Api. And this date is in format YYYY-MM-DD

Marcin Domorozki
  • 51
  • 1
  • 3
  • 12
  • I'm afraid than not. It's true that you can create your custom form adapter to mannage the dates as string, see https://stackblitz.com/edit/angular-64knsp?file=app%2Fdatepicker-adapter.html, but not work with [startDate] – Eliseo Jun 17 '20 at 12:27

1 Answers1

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}} 
emihud
  • 138
  • 8