I'm using Angular 4 with reactiveforms, momentjs, and primeng calendar I'm tying to use setValue
and have tried patchValue
on a reactiveForm field which contains a Date. This date has been created via a primeng calendar.
purchaseDate: Sat Sep 02 2017 00:00:00 GMT+0100 (GMT Daylight Time)
I use this 'date' to do a couple of things and then onSumbit of the form I convert the date using momentjs
to a clean format ready for the backend to accept (i.e. YYYY.MM.DD) using.moment().format(....
However when I run the .setValue
I'm getting the following console error ERROR Missing number at position 0
and can't figure out why.
// convert the date
let newDate = moment(this.form.get('purchaseDate').value).format('YYYY.MM.DD');
// let newDate = moment(this.form.get('purchaseDate')).format('YYYY.MM.DD');
// with or without .value - display the same below (2017.09.01)
console.log(newDate); // 2017.09.01
this.form.get('purchaseDate').setValue(newDate);
// if I create a seperate (empty) reactiveForms field to test against
this.form.get('testField').setValue(newDate) // this works fine
I have traced the issue down to when i try to set / patch the primeng calendar value - for some reason is doesn't like to be changed.
UPDATED monent format
The issue seams to be happening on the setValue now getting the following error
Unexpected literal at position 2 at viewWrappedDebugError
moment(this.form.get('purchaseDate').value).format('yy.MM.dd');
– JayChaseformat
does not accept lowercasey
as year token, useY
,YY
orYYYY
instead. Moreover note that lowercasedd
token stands for day of the week (Su
Mo
...Fr
Sa
), use uppercaseDD
if you want day of the month (01
02
...30
31
) – VincenzoCERROR Error: Unexpected literal at position 2
– fidev.value
makes no difference, I'm loggingnewDate
out and the same appears with or without.value
– fidevnewDate
a moment object (let newDate = moment(this.form.get('purchaseDate').value)
) and then set the value ofpurchaseDate
as Javascript Date (using momenttoDate()
:this.form.get('purchaseDate').setValue(newDate.toDate());
)? – VincenzoC