0

I am very new to Angular and Angular Material.

For the dates my backend wants it to be in zonedDate Format. (e.g.: 2018-04-11T02:12:04.455Z[UTC])

So I am getting value in the above format, which is not binding to mat-datepicker.

Here is html code:

<mat-form-field class="fx-cell-1" floatLabel="never">
    <input matInput name="myDate" [matDatepicker]="myDate" placeholder="Date of Expense"
    [(ngModel)]="myDate" #myDate="ngModel" [max]="maxDate" required >
    <mat-datepicker-toggle matSuffix [for]="myDate"></mat-datepicker-toggle>
    <mat-datepicker #myDate></mat-datepicker>
</mat-form-field>

So here ngModel is not binding value to datepicker.

After some more observation, I realized that:

The date with value "2018-04-11T02:12:04.455Z[UTC]" is binding to the datepicker.

The date with value "2018-04-02T14:00Z[UTC]" is not binding to the datepicker.

Any suggestion???

3
  • if your date is a simple string, did you try to wrap it into a new Date('myDate') before using it in your datepicker? Commented Apr 11, 2018 at 7:08
  • Yes, I tried. But it always gives an error saying "Invalid Date". Commented Apr 11, 2018 at 23:53
  • I tried both strings in new Date(), but both saying Invalid Date. After removing the [UTC] from the string, both would be correctly read as a date time instance in UTC time. So simply strip of the brackets part and everything should work. Commented Apr 12, 2018 at 6:02

1 Answer 1

1

So, basically, you can convert this date with javascript function toISOString().

or just by creating new Date() object.

In my case date was converting into some wrong format and so was getting an error.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.