Display the time in the user's time zone

Here is an example of how to display a GMT (UTC) time, for example from the web server, in the user's time zone, using JavaScript:

<script type="text/javascript">
var time_now = new Date ();
// The difference in minutes
var offset = time_now.getTimezoneOffset () * -1;
var gmt_hours = 21;
var gmt_minutes = 30;
document.write (gmt_hours + ":" + gmt_minutes + 
                " Greenwich Mean Time in your timezone is ");
var total_minutes = gmt_hours * 60 + gmt_minutes;
var your_time = total_minutes + offset;
var day = 24 * 60;
if (your_time < 0)
    your_time += day;
else if (your_time > day)
    your_time = your_time % day;
var your_hours = Math.floor (your_time / 60);
var your_minutes = your_time % 60;
var print_time = your_hours + ":" + your_minutes + ".";
document.write (print_time);
</script>

The offset is calculated using the getTimezoneOffset facility of JavaScript's Date object.

Now we run the above program:


Copyright © Ben Bullock 2009-2017. All rights reserved. For comments, questions, and corrections, please email Ben Bullock (benkasminbullock@gmail.com). / Privacy / Disclaimer