Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert '[object Object]' to date angular 4

How can i convert '[object Object]'(datepicker) to date format

<input type="text"
       id="date"
       class="form-control"
       formControlName="date"
       name="date"
       [(ngModel)]="date"
       ngbDatepicker
       #incorporatedDatePicker="ngbDatepicker"
       (click)="incorporatedDatePicker.toggle()"
       readonly>

when i display

    {{meeting.date | date }} 

i have error: invalidPipeArgument Error '[object Object]' for pipe 'DatePipe'

like image 362
Aya Abdelaziz Avatar asked Aug 28 '17 12:08

Aya Abdelaziz


2 Answers

For those still looking for an answer to this, you have to use the NgbDateAdapter provider like shown in the docs:

import { NgbModule, NgbDateAdapter, NgbDateNativeAdapter } from '@ng-bootstrap/ng-bootstrap';

@NgModule({
  imports: [
    NgbModule.forRoot(),
    ...
  ],
  providers: [{
    provide: NgbDateAdapter,
    useClass: NgbDateNativeAdapter
  }]
})
export class AppModule { }

Then when using [(ngModel)]="from" on the input it will automatically use the model as a Date

like image 174
Guillaume Avatar answered Sep 19 '22 22:09

Guillaume


Based on your comment, what you are trying to feed to the DatePipe is...

{ "day": 18, "month": 8, "year": 2017 }

Angular cannot read an object like that and understand that is a date. You need to transform it before to some format that Angular can understand. Formats that are accepted is mentioned in docs:

date_expression | date[:format]

expression is a date object or a number (milliseconds since UTC epoch) or an ISO string (https://www.w3.org/TR/NOTE-datetime).

like image 26
AT82 Avatar answered Sep 19 '22 22:09

AT82