2

I want Client timezone name (e.g. India Standard Time) in ASP.Net MVC Application.

So, I Can use TimeZoneInfo.FindSystemTimeZoneById(TimeZoneName) function.

I have tried with new Date().getTimezoneOffset() (reference)

But, there is problem with Daylight Saving Time in this Code.

In javascript, i have also tried with new Date().toString()

But, this gives result in different format with different browser.

In firefox, chrome & safari, Answer is "Tue May 08 2012 14:00:00 GMT+0530 (India Standard Time)"

In IE8, it is "Tue May 8 14:00:00 UTC+0530 2012"

Similar problem with opera also.

So, this method will not work for IE & Opera.

What will be best way to identify Client TimeZone?

3 Answers 3

3

the best and the most utilized way is to identify clients location thru some 3rd party library and database utilizing the ip of the client.. and then based upon the location of the client decide the timezone

Sign up to request clarification or add additional context in comments.

Comments

3

Do you need to know the timezone or just the offset from UTC? Because timezones are not an ISO standard and can be changed almost arbitrarily, I prefer to work with the client's offset from UTC and I always get it from the client at login time or whenever they submit a form on which I need the client time. Sometimes I work out the GTM offset of the client and persist it to use wherever I display or use a time.

So I have a global JavaScript function like this which returns the given local date as a universal string:

    function aspClientDateTime(dateObject) {
    return dateObject.getFullYear() + '-'
        + this.leadingZero((dateObject.getMonth() + 1), 2) + '-'
        + this.leadingZero(dateObject.getDate(), 2) + ' '
        + this.leadingZero(dateObject.getHours(), 2) + ':'
        + this.leadingZero(dateObject.getMinutes(), 2) + ':'
        + this.leadingZero(dateObject.getSeconds(), 2);
    }

On form submit, I get the current client date and store it in the form:

myForm.clientTime.val(aspClientDateTime(new Date()));

You then know what time it is in the browser and you can work out UTC offsets etc on the server.

The problem with timezones is that any government could introduce a new one tomorrow, as Samoa recently did. Typically I want to know what the offset of the user is right now, not what timezone they are in.

If it is the latter you really need, you might have to ask them, in the same way that Skype does it.

4 Comments

I don't want client time, I want Client timezone. TimeOffset is not suitable as I mentioned earlier because of daylight saving time.
What I am saying is that timezones are not reliable. If you follow the link to the library suggested by Jon Nylander, it says this: "If you are looking for historically relevant and/or geographically precise timezones. jsTimezoneDetect is not for you. This script does not do geolocation. It simply picks the most populated timezone among several identical ones (in modern time). For example, it is impossible to distinguish between different timezones in central Europe. This script will return Europe/Berlin regardless of where you live in the Central European timezone."
Now, you can do exactly what that script does by capturing the client time and using the C# TimeZoneInfo class to pick a timezone that matches the client's time offset, but you are still taking a guess. This is why I said, if you need to explicitly know their timezone, you should ask them. I'll be interested though if someone comes up with a better solution.
Also, you can get their geolocation using navigator.geolocation.getCurrentPosition (although that is itself unreliable) and you could possibly combine that with their known time offset, but it is all unreliable. The other thing you have to consider is that people (particularly international traders) like to work in a different timezone to the one they live in. So, if possible, ask the user to choose a timezone.
0

There is a library for detecting the timezone through JavaScript. It'll give you the name of a good enough timezone. It does not do geolocation or ip-lookup so it is very fast, but it is not extremely exact. For example it'll return "Europe/Berlin" for anyone in the Central European Timezone. It should be good enough for server side datetime normalizations however.

https://bitbucket.org/pellepim/jstimezonedetect

2 Comments

This is not completely helpful, For India & Sri Lanka, It will be showing "Asia/Calcutta", but I want two different results
It depends on what problem you want to solve. If you want to do datetime calculations across timezones it is extremely helpful. Since Sri Lanka and India have identical timezones. If you're looking for a more geographically precise timezone identifier you will need to use geolocation and/or IP lookups, neither of which are totally reliable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.