Home > Enterprise >  Angular MD Datepicker not patching value
Angular MD Datepicker not patching value

Time:08-07

I've been creaking my head on this issue, looking for answers but couldn't find anything.

I'm using Angular Material on my project and I'm using reactive forms. One of the fields on my form is a Datepicker, the problem is that I can't patch the value. I get the date from the db as number, for example: 1659495600000. The problem is that the field always shows blank. Do I have to convert that to something else?

private initializeForm() {
 this.userForm.controls['shippingDate'].patchValue(this.data.user.shipping.shippingDate);
}

/// The value for this (this.data.user.shipping.shippingDate) is 1659495600000 as a number.

Thanks

CodePudding user response:

MrRobot!

Your db return date in milliseconds, you can do converting to date and after pass the value converted. Exemple to convert your date:

var currentDate = 1659816321923;
document.write("Current date and time in milliseconds: ",currentDate);
var numDate= new Date(currentDate);
document.write("<br> Milliseconds converted to Date format: ",numDate);

CodePudding user response:

Change this line:

  this.userForm.get['shippingDate'].patchValue(this.formatDate(date));

use this function to convert date:

  private formatDate(date) {
   const d = new Date(date);
   let month = ''   (d.getMonth()   1);
   let day = ''   d.getDate();
   const year = d.getFullYear();
   if (month.length < 2) month = '0'   month;
   if (day.length < 2) day = '0'   day;
   return [year, month, day].join('-');
  }
  • Related