Aya Abdelaziz
Aya Abdelaziz

Reputation: 375

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'

Upvotes: 5

Views: 8618

Answers (4)

david alvarado
david alvarado

Reputation: 41

try this:

{{meeting.date.seconds * 1000 | date:'MM-dd-yyyy'}} 

Upvotes: 0

Guillaume
Guillaume

Reputation: 3051

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

Upvotes: 6

B Kansara
B Kansara

Reputation: 1158

In Component

export class DateComponent implements OnInit {
      objDate = Date.now();
}

In HTML

<strong>{{ objDate | date :'fullDate' }}</strong>

OutPut

Thursday, February 15, 2018

Upvotes: 0

AVJT82
AVJT82

Reputation: 73357

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).

Upvotes: 1

Related Questions