Over a million developers have joined DZone.

Time Zone Calculator

Check out this nifty time zone calculator demonstrated using jQuery and a Google API.

· Web Dev Zone

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

In this post, we will try to create a time zone calculator. We will be calculating time zones in two ways:

  • Using normal jQuery functions
  • Using the Google API
  • We will be using Visual Studio as our IDE. For the jQuery implementation, we will be creating some prototypes.

    Download Source Code

    You can always download the source code from here: Time Zone Calculator.


    In one of my projects, we were saving some date values in UTC format on the server. When we returned the data we were not doing any calculation like converting the date values to user location time—if it is India UTC – IST. Therefore I was asked to get the time zone values from the client side and pass the same value to the server in each request. I had done this, and here I will show you how. As I explained above, we can do this in two ways.

  • Using normal jQuery functions
  • Using Google API
  • We will be explaining these two methods one by one.

    Using the Code

    First of all, create a new project in Visual Studio and install jQuery and Bootstrap from NuGet package manager. Once your project is ready, we will start implementing our first method.

    Using jQuery Functions

    The first thing you need to do here is adding the needed elements to UI.

    <div class="col-sm-6">
                 <h3>Using jQuery prototype function</h3>
                 <p>Here we are going to use prototype functions we created</p>
                 <div class="form-group">
                     <label class="label label-primary">Please select date:</label>
                     <br />
                     <br />
                     <input class="form-control" type="text" id="txtTimeZone" />
                     <br />
                     <br />
                     <input type="button" value="Submit" id="btnSubmit" class="btn btn-primary btn-block"/>
                     <br />
                     <div class="panel panel-default">
                         <div id="placeholder" class="panel-body"></div>

    Please add the needed references to your page.

    <link href="Content/themes/base/all.css" rel="stylesheet" />
    <link href="Content/bootstrap.min.css" rel="stylesheet" />
        <link href="Content/Index.css" rel="stylesheet" />
        <script src="Scripts/jquery-2.2.3.min.js"></script>
        <script src="Scripts/jquery-ui-1.11.4.min.js"></script>

    Now, we are going to create some date prototypes to a js file TimeZone.js so that we can use it with any date objects.

    Date.prototype.dst = function () {
        return this.getTimezoneOffset() < this.stdTimezoneOffset();

    The above code is for finding whether daylight time is ON or OFF If it returns true it means that daylight time is OFF.

    Date.prototype.stdTimezoneOffset = function () {
        var jan = new Date(this.getFullYear(), 0, 1);
        var jul = new Date(this.getFullYear(), 6, 1);
        return (Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset()) * -1);

    The above prototype can be called when the daylight time is OFF.

    Date.prototype.stdTimezoneOnset = function () {
        var jan = new Date(this.getFullYear(), 0, 1);
        var jul = new Date(this.getFullYear(), 6, 1);
        return (Math.min(jan.getTimezoneOffset(), jul.getTimezoneOffset()) * -1);

    The above prototype can be called when the daylight time is ON.

    Now we have created all the prototypes needed. We can use them now.

    $(document).ready(function () {
        $('#btnSubmit').click(function () {
            var dayLight = new Date(); 
            dayLight = new Date($('#txtTimeZone').val());
            if (dayLight.dst()) {
                timeZoneValue = dayLight.stdTimezoneOffset();// Day light time is OFF
            } else {
                timeZoneValue = dayLight.stdTimezoneOnset();// Day light time is ON
            $("#placeholder").text('The time zone value for ' + dayLight + ' is ' + timeZoneValue);

    Please run your page now to check the output.

    Image title

    Using Google API

    Before we start, please get your own map API key from Google. You can follow this link to get one.

    Please add some controls as below to your page.

    <div class="col-sm-6">
                    <h3>Using Google API</h3>
                    <p>Here we are going to use Google API</p>
                    <div class="form-group">
                        <input id="pac-input" class="controls" type="text" placeholder="Search Box">
                        <div id="map"></div>
                        <script async defer src="https://maps.googleapis.com/maps/api/js?key=yourkey&libraries=places&callback=initAutocomplete" type="text/javascript"></script>
                        <div class="panel panel-default">
                            <div id="latlon" class="panel-body"></div>

    Here initAutocomplete is the callback function which we are going to write now.

    function initAutocomplete() {
        var map = new google.maps.Map(document.getElementById('map'), {
            center: { lat: -33.8688, lng: 151.2195 },
            zoom: 13,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        // Create the search box and link it to the UI element.
        var input = document.getElementById('pac-input');
        var searchBox = new google.maps.places.SearchBox(input);
        // Bias the SearchBox results towards current map's viewport.
        map.addListener('bounds_changed', function () {
        var markers = [];
        // Listen for the event fired when the user selects a prediction and retrieve
        // more details for that place.
        searchBox.addListener('places_changed', function () {
            var places = searchBox.getPlaces();
            if (places.length == 0) {
            // Clear out the old markers.
            markers.forEach(function (marker) {
            markers = [];
            // For each place, get the icon, name and location.
            var bounds = new google.maps.LatLngBounds();
            places.forEach(function (place) {
                var icon = {
                    url: place.icon,
                    size: new google.maps.Size(71, 71),
                    origin: new google.maps.Point(0, 0),
                    anchor: new google.maps.Point(17, 34),
                    scaledSize: new google.maps.Size(25, 25)
                // Create a marker for each place.
                markers.push(new google.maps.Marker({
                    map: map,
                    icon: icon,
                    title: place.name,
                    position: place.geometry.location
                $('#latlon').text(place.geometry.location.lat() + ',' + place.geometry.location.lng() + ', The time zone value is ' + place.utc_offset);
                if (place.geometry.viewport) {
                    // Only geocodes have viewport.
                } else {

    Next is the custom code we have written for showing the time zone values to our text box.

    $('#latlon').text(place.geometry.location.lat() + ',' + place.geometry.location.lng() + ', The time zone value is ' + place.utc_offset);

    Now it is time to add our styles.

    #map {
        width: 100%;
        height: 200px;
        background-color: #CCC;
    .controls {
        margin-top: 10px;
        border: 1px solid transparent;
        border-radius: 2px 0 0 2px;
        box-sizing: border-box;
        -moz-box-sizing: border-box;
        height: 32px;
        outline: none;
        box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
    #pac-input {
        background-color: #fff;
        font-family: Roboto;
        font-size: 15px;
        font-weight: 300;
        margin-left: 12px;
        padding: 0 11px 0 13px;
        text-overflow: ellipsis;
        width: 300px;
        #pac-input:focus {
            border-color: #4d90fe;
    .pac-container {
        font-family: Roboto;
    #type-selector {
        color: #fff;
        background-color: #4d90fe;
        padding: 5px 11px 0px 11px;
        #type-selector label {
            font-family: Roboto;
            font-size: 13px;
            font-weight: 300;
    #target {
        width: 345px;

    Please note that if you don’t set height for the map container div, the map may not get loaded. I have given the CSS for my map container as follows.

    #map {
        width: 100%;
        height: 200px;
        background-color: #CCC;

    Now, please run your page and you should see a map. Search for any location, and it will give you the timezone value for the chosen location.

    Image title

    Image title

    Please make sure that the value you get from both is the same.


    Did I miss anything? Please share any suggestions and feedback you might have.

    Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.

    jquery,time zones,google api

    Published at DZone with permission of Sibeesh Venu, DZone MVB. See the original article here.

    Opinions expressed by DZone contributors are their own.

    The best of DZone straight to your inbox.

    Please provide a valid email address.

    Thanks for subscribing!

    Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

    {{ parent.title || parent.header.title}}

    {{ parent.tldr }}

    {{ parent.urlSource.name }}