0

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

1
  • Can you add HTML code for the datepicker? Commented Aug 6, 2022 at 20:50

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

1 Comment

Hey @Pedro thanks for the help, but I had already done that and nothing :/
0

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('-');
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.