ASP.Net Time Localization

Iโ€™m working on a site that allows users to add calendar entries, on the main page all users can see these entries with some text saying how long until these entries are current, for example

Intro 1: 5 minutes
Entry 2: 7 hours
Record 3: 4 days

The problem I am facing is that everyone can be in different time zones. I decided that I want to store all time / dates in GMT.

What I encounter is when a user sends a calendar entry, say, for 01.01.2009 10:30. Then I need to find out what time it is in GMT for storage. Is there a way to find out what the user's local time zone / time is, so I can compare it with server time (GMT) to do the conversion?

I really hope this makes sense. I hope that I can do this without asking the user what time zone they are in, or forcing the user to enter all the data in GMT.

+6
timezone localization
source share
3 answers

Map the date-time to the GMT client (in JavaScript, if it is a web application) before sending it to the server. Let the server work only with GMT (I would say UTC). The same is for date / time display. Send GMT / UTC to the client and let JS localize. In my opinion, this is the only safe way, because you allowed it to work with the users operating system to figure out what time should be.

There are other approaches to business, and if you only need to know what time the client is now, you can just get the offset from him (can be obtained through JS, etc.). But if you need exact dates / times in different places for historical and future dates / times, you can let the client handle this.

PS: In my implementation of TimeZone, I also use the UNIX time format for communication between the client and server. It's just easier to parse, and I believe this is the native date format for JS. I canโ€™t present my code for you now, but for this it should be relatively simple.

+5
source share

Your only option (besides the user's explicit choice) is to get the time zone offset from the browser:

var tzOffset = new Date().getTimezoneOffset(); // Minutes 

Then you need to send it to the server using a hidden field or other suitable approach.

This will give you only the time zone offset, not the full time zone information. This is not very reliable, so I would still leave the choice to the user, but I use this information to pre-select the appropriate time zone.

+1
source share
+1
source share

All Articles