.NET/Answer Detail

How to get client machine time zone?

beginer
18 Points
9 Posts

Hi everyone,

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

Total Answered: 2
Total Marked As Answer: 0
Posted On: 13-Oct-2017 06:14
I think, JavaScript getTimezoneOffset() Method may help to get timezone - Priya  13-Oct-2017 06:18
Answers
Jak
Member
332 Points
146 Posts
 
 
Posted On: 17-Oct-2017 05:02

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.

Rashmi
Member
24 Points
12 Posts
 
 
Posted On: 19-Oct-2017 02:20

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);
}

When replying / answering a question please:

  1. Read the question and other answers carefully.
  2. Understand that English isn't everyone's first language so be sympathetic of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for explanation, ignore it. rudeness is not allowed.
Let's work to help programmers, not make them feel stupid.

Blog

Active User (2)

 Log In to Chat