material-ui icon indicating copy to clipboard operation
material-ui copied to clipboard

automatic DateTimeFormat based on users language

Open flyon opened this issue 10 years ago • 3 comments

Im trying to get DatePicker to automatically use the format of the language of the user. I'm developing a chrome plugin, so I can use navigator.language which for me returns 'nl' and new Intl.DateTimeFormat(navigator.language).format returns a function, but this does not work.

Any advice how I can get this work?

flyon avatar Dec 08 '15 07:12 flyon

Can't you just the an Intl polyfill? We have an example on the doc DatePicker source code.

oliviertassinari avatar Dec 08 '15 08:12 oliviertassinari

Yes, I think the best way to achieve this is to use Intl.

Take a look at the docs http://www.material-ui.com/#/components/date-picker or this example https://github.com/callemall/material-ui/issues/2409#issuecomment-162868427

You will need to add the polyfill:https://github.com/andyearnshaw/Intl.js/

almoraleslopez avatar Dec 08 '15 19:12 almoraleslopez

No don't need the polyfill, as it's chrome only.

Found the solution though. I coulnd't put together how to use Intl.DteTimeFormat together with the dateFormat property before, but I figured it out now (and ofcourse its quite simple). Tnx for the help

formatDate(date)
{
    return new Intl.DateTimeFormat(navigator.language).format(date);
}
...
<DatePicker dateFormat={this.formatDate.bind(this} ... />

flyon avatar Dec 08 '15 19:12 flyon