angularangular5primengangular-bootstrap-calendar

How to set a default date and time to PrimeNG p-calendar


I have included p-calendar in my project with showTime="true" :

  <p-calendar formControlName="pre_check_in" [defaultDate]="defaultDate"  [minDate]="dateTime" showTime="true" hourFormat="24" showButtonBar="true"    required>

Then in my TS file i have used setValue to assign data fetched from database to the respected formControlls:

 private passService(qsUserId) {
    this._appService.getEditVisitData(qsUserId).subscribe((data) => {
      console.log(data);
      this.editCheckinData = data;

      this.editCheckinForm.controls[ 'id' ].setValue(this.editCheckinData[ 'id' ]);
      this.editCheckinForm.controls[ 'f_name' ].setValue(this.editCheckinData[ 'f_name' ]);
      this.editCheckinForm.controls[ 'l_name' ].setValue(this.editCheckinData[ 'l_name' ]);
      this.editCheckinForm.controls[ 'purpose' ].setValue(this.editCheckinData[ 'purpose' ]);
      this.editCheckinForm.controls[ 'department' ].setValue(this.editCheckinData[ 'department' ]);
       this.editCheckinForm.controls[ 'pre_check_in' ].setValue(this.editCheckinData[ 'pre_check_in' ]);
     // this.date1 = this.editCheckinData[ 'pre_check_in' ];
    });

  }

Other fields show the desired data but the p-calendar still remains empty. Is it possible to show the data in the p-calendar as default date. The data that is been fetched to display in the form is in the following format:

check_in:null
checkin_status:false
created_at:"2018-04-12T04:56:43.000Z"
creator_email:"atul.vurung@gmail.com"
creator_id:"atul.vurung@gmail.com"
creator_mobileno:0
department:"D0001"
entry_source:"admin"
f_name:"erd"
id:43
l_name:"erd"
organization:"ORG_VURUNG"
pre_check_in:"2018-04-12T04:56:20.744Z"
purpose:"discuss"
updated_at:"2018-04-12T04:56:43.000Z"
visitor_type:"visitor"

Solution

  • Try this

    const formattedDate=new Date(this.editCheckinData[ 'pre_check_in']);
    this.editCheckinForm.controls[ 'pre_check_in' ].setValue(formattedDate);
    

    I hope this will work for you. If you have any issues let me know.