On my site, I need to know what timezones people are located in, in order to display messages to them at the right times. I am not too sure what to be searching for in terms of a tutorial on how to do this.
What I am planning on doing is: when a user comes to my site, they will set their timezone by selecting it from a dropdown list. I will store their settings in my database and use it to calculate the times.
I am not sure what I need. What should be my database time be storing? I read somewhere that it should be stored as UTC. How do I set up my database to do this? Also, I will be on a shared hosting site so it has to be set in a way that I can do it (maybe through webconfig?).
Next, I would need a list of all the timezones as either an HTML helper or regular HTML. I really don't want to make one.
Then, like I said, I need a tutorial showing me what to do. I think it is adding and subtracting stuff from that time, but I could be wrong.
In this article, we will see how to convert time from one time zone to another, in an ASP.NET application. Step 1: Fire up Visual Studio > File > New Website. Drag and drop a DropDownList and two Label controls to the page and add a ‘SelectedIndexChanged’ event to the DropDownList (we will code this event later).
The list of timezones is already available in .Net, so you can see this post on how to enumerate them. For ASP.Net MVC, instead of printing the time out, you would want to assign the converted datetime to a property of your model class so your View could use it for display.
If you need to handle time zone changes while your application is running, use the TimeZoneInfo class and call its ClearCachedData () method. Instantiates a new TimeZoneInfo object based on its identifier. Converts a date and time to Coordinated Universal Time (UTC).
I would recommend to always use UTC (GMT) time on the server side (in code-behind, database, etc), and convert time from UTC to local time for display purposes only. This means that all time manipulations - including saving time in database, performing calculations, etc - should be be done using UTC.
For this purpose, you should definitely be storing your timestamps in UTC in the database.
When you need to display a date from the database on your site, you can do this:
DateTime stamp = /* get datetime from the database here, make sure you
use the constructor that allows you to specify the
DateTimeKind as UTC. */
//E.g.
//DateTime stamp = new DateTime(2009, 12, 12, 12, 12, 12, DateTimeKind.Utc);
timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(" /* users time zone here */");
var convertedTime = TimeZoneInfo.ConvertTime(stamp, timeZoneInfo);
//Print out the date and time
//Console.WriteLine(convertedTime.ToString("yyyy-MM-dd HH-mm-ss"));
The list of timezones is already available in .Net, so you can see this post on how to enumerate them. For ASP.Net MVC, instead of printing the time out, you would want to assign the converted datetime to a property of your model class so your View could use it for display.
I cannot stress how important it is to use UTC for storage and processing for any application you write. Even if you know the application will ever only be used in a single timezone it is still much easier to work with UTC than a local timezone because of daylight saving time issues.
There's really nothing special you need to do at the database level. Just create a normal datetime column, but make sure it is documented clearly that the column is assumed to be UTC.
I'll be honest, asp.net isn't my expertise, but I'm thinking you could obtain the timezone from the client request somehow. Actually, because the daylight saving time rules can be pretty obscure depending on the region it might be better to use a java script that calculates the UTC offset and use that to make the conversions. Even after the Energy Policy Act of 2005 there are still some exceptions to the DST rules that would rather hard to deal with on the server side. However, I think you're idea of allowing the client to set their own timezone would work well in most cases.
First, check MSDN for DateTime documentation and read up on it. Here is an article on Best Practices with DateTime that covers timezones and UTC.
Yes, you should be storing UTC in your database. Storing DateTimes as UTC will allow you to translate times to (and from) users based on their selected timezone. You're on the right track!
Usage
getCurrentDateTimeWithTimeZone("Central Europe Standard Time");
Method
public DateTime getCurrentDateTimeWithTimeZone(string strTimeZone)
{
var localTimezone = TimeZoneInfo.Local;
var userTimezone = TimeZoneInfo.FindSystemTimeZoneById(strTimeZone);
var todayDate = DateTime.Now;
var todayLocal = new DateTimeOffset(todayDate,
localTimezone.GetUtcOffset(todayDate));
var todayUserOffset = TimeZoneInfo.ConvertTime(todayLocal, userTimezone);
return todayUserOffset.DateTime;
}
List of Timezone ID
Dateline Standard Time
UTC-11
Samoa Standard Time
Hawaiian Standard Time
Alaskan Standard Time
Pacific Standard Time (Mexico)
Pacific Standard Time
US Mountain Standard Time
Mountain Standard Time (Mexico)
Mountain Standard Time
Central America Standard Time
Central Standard Time
Central Standard Time (Mexico)
Canada Central Standard Time
SA Pacific Standard Time
Eastern Standard Time
US Eastern Standard Time
Venezuela Standard Time
Paraguay Standard Time
Atlantic Standard Time
Central Brazilian Standard Time
SA Western Standard Time
Pacific SA Standard Time
Newfoundland Standard Time
E. South America Standard Time
Argentina Standard Time
SA Eastern Standard Time
Greenland Standard Time
Montevideo Standard Time
UTC-02
Mid-Atlantic Standard Time
Azores Standard Time
Cape Verde Standard Time
Morocco Standard Time
UTC
GMT Standard Time
Greenwich Standard Time
W. Europe Standard Time
Central Europe Standard Time
Romance Standard Time
Central European Standard Time
W. Central Africa Standard Time
Namibia Standard Time
Jordan Standard Time
GTB Standard Time
Middle East Standard Time
Egypt Standard Time
Syria Standard Time
South Africa Standard Time
FLE Standard Time
Israel Standard Time
E. Europe Standard Time
Arabic Standard Time
Arab Standard Time
Russian Standard Time
E. Africa Standard Time
Iran Standard Time
Arabian Standard Time
Azerbaijan Standard Time
Mauritius Standard Time
Georgian Standard Time
Caucasus Standard Time
Afghanistan Standard Time
Ekaterinburg Standard Time
Pakistan Standard Time
West Asia Standard Time
India Standard Time
Sri Lanka Standard Time
Nepal Standard Time
Central Asia Standard Time
Bangladesh Standard Time
N. Central Asia Standard Time
Myanmar Standard Time
SE Asia Standard Time
North Asia Standard Time
China Standard Time
North Asia East Standard Time
Singapore Standard Time
W. Australia Standard Time
Taipei Standard Time
Ulaanbaatar Standard Time
Tokyo Standard Time
Korea Standard Time
Yakutsk Standard Time
Cen. Australia Standard Time
AUS Central Standard Time
E. Australia Standard Time
AUS Eastern Standard Time
West Pacific Standard Time
Tasmania Standard Time
Vladivostok Standard Time
Central Pacific Standard Time
New Zealand Standard Time
UTC+12
Fiji Standard Time
Kamchatka Standard Time
Tonga Standard Time
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With