I use reactive forms in my Angular 2 webapp and I am having problems setting the date of ngbDatepicker (ngbootstrap 1 alpha 6). My object has a date object, for example:
var myObject = {date: new Date(1, 9, 2016)};
and in my reactive form it is configured as follows:
input.form-control(name='date', ngbDatepicker, #date="ngbDatepicker", placeholder='jj.mm.aaaa', formControlName='date', type="text")
and I correct the form as follows:
this.form.patchValue({myObject: myObject});
The problem is that ngbDatepicker accepts a date with the following structure:
{day: DD, month: MM, year: YYYY}
I found a workaround that does:
this.form.controls.myObject.controls.date.valueChanges .map((value) => { if(value) { if (typeof value.getMonth === 'function') { this.form.controls.myObject.patchValue({ date: { day: value.getUTCDay(), month: value.getUTCMonth(), year: value.getUTCFullYear() } }); } } return value; }) .subscribe((value) => { });
And everything works as expected (the date is updated whenever the form is encrypted), but it is too verbose (18 lines of code), and my form has a dozen dates!
So my question is: can I achieve the same result with a much shorter solution?