0

I m trying to implement a method which can understand which coutry you are living and show time time exactly for this country. for example if you are living in USA showing the time with am/pm but if you are living in the Turkey showing the time for 24 hour format. I found a way to conver 24 hour system to am/pm but after that i got clueless.. Any ideas how to achieve that ?

function formatAMPM(date) {
  var hours = date.getHours();
  var minutes = date.getMinutes();
  var ampm = hours >= 12 ? 'pm' : 'am';
  hours = hours % 12;
  hours = hours ? hours : 12; // the hour '0' should be '12'
  minutes = minutes < 10 ? '0'+minutes : minutes;
  var strTime = hours + ':' + minutes + ' ' + ampm;
  return strTime;
}
Akin Dönmez
  • 353
  • 8
  • 24

1 Answers1

0

I think, you can use toLocaleTimeString method to show time exactly for that country:

var d = new Date();
d.toLocateTimeString('en-US'); // "12:43:04 PM"
d.toLocaleTimeString("ja-JP"); // "12:43:04"

Here's link to MDN
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleTimeString

I hope my answer will help you.
May the force be with you.

  • So the problem is how am i get 'en-US' or 'ja-JP' ? is there any method which gives me that when a call ? – Akin Dönmez May 25 '16 at 09:50
  • @AkinDönmez you may use navigator.language or navigator.languages, if you want to get the country code from the javascript – Eugene Korobov May 25 '16 at 09:52
  • 1
    @AkinDönmez, well... seems like navigator isn't good enough, here's good answer i've already found on StackOverflow http://stackoverflow.com/questions/17680413/get-visitors-language-country-code-with-javascript-client-side – Eugene Korobov May 25 '16 at 09:53