Locale detection with Moment.js

2019-01-08 11:24发布

I am using Moment.js in my project and formatting dates as follows:

var date = moment.unix(1318781876);
return date.format('LLLL');

The moment docs state the multiple locales are supported. I would like to know if moment.js will auto-detect the locale, or do I need to detect the locale and pass it to moment?

Update My goal is to ensure the displayed date is in the format of the user's region. i.e. in the US the short date format is mm/dd/yy whereas in the UK it is dd/mm/yy

标签: momentjs
1条回答
【Aperson】
2楼-- · 2019-01-08 11:48

As of momentjs documentation:

By default, Moment.js comes with English locale strings. If you need other locales, you can load them into Moment.js for later use.

You can change it with that:

moment.locale(locale);

To get the user's locale with javascript you can do that:

var locale = window.navigator.userLanguage || window.navigator.language;

Refer to: http://momentjs.com/docs/#/i18n/changing-locale/

and JavaScript for detecting browser language preference

查看更多
登录 后发表回答