Angular2 - Setting date field on reactive form

后端 未结 4 431
星月不相逢
星月不相逢 2020-11-29 10:12

I have a component that uses two date fields, a start date & and end date.

By default, I have my end date field disabled and I toggle it when they select a start

4条回答
  •  星月不相逢
    2020-11-29 11:01

    When you run this code in Chrome (other browsers not tested) it logs an error to console:

    The specified value "7/26/2017" does not conform to the required format, "yyyy-MM-dd".

    Which I think describes the problem pretty well

    You can fix it by changing your currentDate() method to something like:

    currentDate() {
      const currentDate = new Date();
      return currentDate.toISOString().substring(0,10);
    }
    

    Live plunker example

    While this does fix the problem the answer from @JGFMK shows a better way of converting the date using a DatePipe

提交回复
热议问题