0

I need to show the material datepicker widget in the UTC timezone since our dates will all be shown in UTC. Since Moment.js hasn't been approved for use yet, I have to find a workaround. I have tried this method link. But it doesn't allow me to change the highlighted day when selected.

mat datepicker

This creates an issue when clicking on the day highlighted because the datepicker doesnt update the model when the selected date is the same day.

5
  • It may help you stackoverflow.com/questions/56578738/… Commented Dec 20, 2019 at 21:31
  • @RafaqatAli Thanks for the article, but this isn't technically the issue. I need the datepicker widget to also use UTC time and not Locale time. Commented Dec 20, 2019 at 23:05
  • 1
    Another post here with two possible answers, one extends material function and the other uses moment.js: stackoverflow.com/questions/54288803/… Commented Jan 2, 2020 at 21:51
  • @NathanielFlick I tried that method, but I had issues with it not updating the selected date on the widget that you can click. It would have the correct date at the top of the widget, but the highlighted day below was off by a day. Commented Jan 2, 2020 at 21:54
  • Just realized that this screenshot is from using that method. As you can see the highlighted day is off. Commented Jan 2, 2020 at 22:49

1 Answer 1

0

I resolved this by adding a "hidden" date field that is tied to the calendar widget that I manually set to the "UTC" timezone. This date is still in browsers timezone, but the time is adjusted to match the UTC timezone so it displays correctly in the widget.

this.hiddenDate = new Date(this.date.getTime() + this.date.getTimezoneOffset() * 60000);

A better solution is to import the momentJS module if you have access to it.

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

1 Comment

remember that for Western and Eastern timezones you need to add and substract timezone offset because JS method getTimezoneOffset always return positive value

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.