How to load a json file based on locale of the browser in angular1.x?

107 Views Asked by At

In my project the selection of browser locale I want to change the date format, how can I get the date format according to the locale in javascript or angularjs

1

There are 1 best solutions below

0
Pooya Estakhri On

in order to detect user time format you can use Moment.js.

after including moment.js library, first detect user locale by this javascript code :

let user_locale = window.navigator.userLanguage || window.navigator.language;

then feed the user_locale value to moment.locale(); function like this

moment.locale(user_locale);

after declaring localData

let localeData = moment.localeData()

you can use LongDateFormat

localeData.longDateFormat(dateFormat); // returns the full format of abbreviated date-time formats LT, L, LL and so on

to get time format.