0

I want to set the moment.locale according to the user country by looking at the browser or timestamp or something. Not sure how this could be achieved.

currently i am achieving like this -

const userLocale = 'ja';
moment.locale(userLocale); 

any ideas awesome folks?

GeekOnGadgets
  • 941
  • 3
  • 14
  • 47
  • You can find infos about getting browser's locale [here](http://stackoverflow.com/q/673905/4131048) and [here](http://stackoverflow.com/q/1043339/4131048) – VincenzoC Nov 24 '16 at 09:11

2 Answers2

0

You could probably use moment-timezone

However, dates in JavaScript always defaults to the user's local timezone. So it sounds like a problem that is solved by default. For example, by running the snippet below I see my local time, which is GMT-0500. You should see something different if you're in another timezone.

But if you want to convert a local timestamp to a specific timezone from a user setting or similar, moment-timezone is the easiest way.

document.getElementById("date").innerHTML = new Date();
<div id="date"></div>
hampusohlsson
  • 10,109
  • 5
  • 33
  • 50
0

You can get Browser's locale by

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

and use that locale to set Moment's locale

moment.locale(locale);

If there's a problem in getting browser's locale try

var locale = navigator.languages && navigator.languages[0] || // Chrome and Firefox
           navigator.language ||   // All browsers
           navigator.userLanguage; // IE <= 10

Hope it helps :)

Rajat Sharma
  • 779
  • 7
  • 6