Detect user timezone using javascript

by  
Share:
Detect user timezone using javascript

I was looking for a script/plugin that could detect user timezone using javasript and display the current date time based on their timezone. So after some googling I came across jsTimezoneDetect and moment.js which does the job perfectly for me.

View Demo

jsTimezoneDetect: Detect user time zone using JavaScript

To detect the user timezone I have used jsTimezoneDetect which return value is an IANA zone info key eg. America/New_York, Asia/Hong_Kong. It has been mentioned by their developers that, this script does not do geo-location, nor does it care very much about historical time zones. So if you are unhappy with the time zone “Europe/Berlin” when the user is in fact in “Europe/Stockholm” – this script is not for you. (They are both identical in modern time). For more info you can visit their official webiste.

<script type="text/javascript" src="jquery-1.9.0.min.js"></script>
<script type="text/javascript" src="jstz-1.0.4.min.js"></script>

<script type="text/javascript">
$(document).ready(function() {
  var tz = jstz.determine();
  var timezone = tz.name();
  alert(timezone);        
});
</script>

moment.js: Display current datetime for user timezone

Moment.js is a fantastic library for parsing, validating, manipulating, and formatting dates. You can find more info visit their offical website.

<script type="text/javascript" src="moment.min.js"></script>
<script type="text/javascript" src="moment-timezone.js"></script>
<script type="text/javascript" src="moment-timezone-data.js"></script>

<script type="text/javascript">
$(document).ready(function() {
  // replace the timezone with user timezone.
  var current_time =  moment().tz("Asia/Hong_Kong").format('MMMM Do YYYY, h:mm:ss a');
  alert(current_time);

});
</script>

Check the demo and let me know if this script worked for you.

View Demo

Share to download the Source Codes for FREE!
We're glad to give free downloads, but we need your love to carry on making that.
Please support us by sharing the page.

Get the latest updates directly in your inbox for FREE: Subscribe here

Share your feedback / let me know your doubts regarding this tutorial in the comment box given below.

Share:
Hasan

Shahrukh Khan (Hasan)

A software engineer who's a die-hard coder, blogger, dreamer and mentor with years of expertise in web development. Know more...

Related Posts

s.shivasurya on

useful js!

Reply
Nandar on

thank you very much. your code is useful for me.

Reply
Shahrukh Khan on

you are welcome

Reply
Vinoth D on

how to get fullname of timezone, for e.g CST as Central Standard Time

Reply
Shahrukh Khan on

You can either copy the list and put it in csv file manually through this link or you can check this tutorial

Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.