DotNet/Answer Detail

How to get client machine time zone?

beginer
beginer
48 Points
19 Posts

Hi everyone,

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

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
348 Points
149 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
34 Points
12 Posts
Marked as answer      

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

Blog

Active User (0)

No Active User!
 Log In to Chat