How to get client machine time zone?

beginer
beginer
84 Points
32 Posts

Hi everyone,

Does anyone know the way to get the client machine time zone?

Views: 8442
Total Answered: 2
Total Marked As Answer: 1
Posted On: 13-Oct-2017 06:14

Share:   fb twitter linkedin
I think, JavaScript getTimezoneOffset() Method may help to get timezone
 - Priya  13-Oct-2017 06:18
Answers
Jak
Jak
Member
364 Points
152 Posts
         

Use JavaScript getTimezoneOffset() Method.  It will return the timezone difference between UTC and Local Time, in minutes. For example, If your time zone is GMT+2, -120 will be returned.

var d = new Date();
var n = d.getTimezoneOffset();

Note: The returned value is not a constant, because of the practice of using Daylight Saving Time.
Tip: The Universal Coordinated Time (UTC) is the time set by the World Time Standard.

Posted On: 17-Oct-2017 05:02
Rashmi
Rashmi
Member
90 Points
25 Posts
         

Do following in client side on page load:

$(document).ready(function () {
    var d = new Date();
    var tZOffset = d.getTimezoneOffset();
    $.ajax({
        url: '/mytimezone/sendTimezoneOffset',
        data: { timezoneOffset: tZOffset },
        dataType: "json",
        success: function (data) {
           console.log(data);
        },
        error: ajaxErrorCallBack
    });
});

On server side calculate the timezone on the basis of offset:

public ActionResult sendTimezoneOffset(double timezoneOffset)
{
    var zone = TimeZoneInfo.GetSystemTimeZones().FirstOrDefault(x => x.BaseUtcOffset.TotalMinutes == -timezoneOffset);
    string timeZoneId;
    if (zone != null && !string.IsNullOrEmpty(zone.Id))
    {
        timeZoneId = zone.Id;
    }
    return Json(timeZoneId);
}

Posted On: 19-Oct-2017 02:20
 Log In to Chat