Searching in input gives incorrect date in material date picker need to convert it into UTC

Viewed 93

I am extending MomentDateAdapter for my requirement. When i am selecting a date from the calendar i am getting the correct output but when i manually type something in the input field i get wrong output.

For the selected date i am using _moment.utc({ year, month, date }).locale(navigator.language); to convert the selected value to UTC format but i am not sure on how to do the same when user searches in the input field.

StackBlitz.

to reproduce:

  1. Try to select a value from calendar and see the console (notice the date is converted to UTC)
  2. Now try to add a date manually by typing in and see the console (date is not converted to UTC).
1 Answers

You need to adapt your parse method call of moment to:

return moment.utc(value, parseFormat, this.locale, true);

to get utc Date from your input.

Here is your adapted Stackblitz.

The methods format and createDate are called if you set your date via picker, the parse method is called if you set it via input.

Related