Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
623 views
in Technique[技术] by (71.8m points)

momentjs - Locale detection with Moment.js

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...