I have an application which potentially rans in different timezones in the world, to name a few it runs in Germany & UK.
Now I have a requirement wherein I have to interpret datetime objects as they are GMT dates, and then get the equivalent UTC datetime out of it. This should happen irrespective of timezones, for example if the application is running in Germany as well the DateTime should be interpreted as GMT and converted to UTC before saving on the server.I 开发者_开发知识库do deal will DST shifts as well.
For ex:
If I have a datetime object like this:
var date = new Datetime(2011,03,28,01,00,00)
, this should be converted to it's equivalent UTC.In this case it should be 28/03/2011 01:00:00 +01:00:00
, however while my server reads the saved datetime it finds it as a 28/03/2011 01:00:00 +02:00:00
. My UI was running in Germany at the moment and I suspect the dates were interpreted as local dates(CET).
Can you please advise me how to perform the accurate conversion?
It's usually considered a best practice to
- Store datetimes as UTC
- Render this in timezone-aware user friendly format as late as possible
Thus, I would suggest you to not rely on DateTime.Now
, but instead consider using DateTime.UtcNow
.
Provided you allow each user to determine (through a Preferences/Options panel) to select its own timezone, then you could render an UTC date in the appropriate user-friendly format using:
string timeZoneInfoId = "Romance Standard Time"; // Or any valid Windows timezone id ;-)
var tzi = TimeZoneInfo.FindSystemTimeZoneById(timeZoneInfoId);
//Build a DateTimeOffset which holds the correct offset considering the user's timezone
var dto = new DateTimeOffset(TimeZoneInfo.ConvertTimeFromUtc(utcDateTime, timeZoneInfo), timeZoneInfo.GetUtcOffset(utcDateTime));
var formated = string.Format("{0} {1} ({2})", dto.ToString("d"), dto.ToString("T"), dto.ToString("zzz")); //Or whatever format that fits you
Note: You can find the list of all valid Windows timeZoneId here.
Provided you're willing to add a selecting list for the user to choose its rendering timezone, you could use TimeZoneInfo.GetSystemTimeZones()
to retrieve a list of TimeZoneInfo
, then use the Id
property of each TimeZoneInfo as the option's value and call the ToString()
method of each TimeZoneInfo to render the option's content.
Your selecting list would render in a similar way than native Windows one:
For the first part of your query , you can get the UTC time from the DateTime object.
DateTime dt = DateTime.Now;
dt.ToUniversalTime().ToString()
will give you UTC time.
Now incase you want to represent the same as local time, you will need to get the offset from the client end via javascript. Something like
var now = new Date();
var offset = now.getTimezoneOffset();
Now you may use this offset to display the Time for the client by adding or subtracting the minutes you get from this method.
Hope this helps.
Take a look at the TimeZoneInfo class: http://msdn.microsoft.com/en-us/library/system.timezoneinfo.aspx
date += TimeZone.CurrentTimeZone.GetUtcOffset(date);
精彩评论