你可以用這篇優秀的文章「World Clock and the TimeZoneInformation class」來補充你的解決方案,我做了一個web服務,發送一個包含本地和接收者時間的信息的文件,我所做的就是修改這個類,以便我可以處理這個問題,它完美無缺,完全按照我的需要。
我想你可以帶上這個課程,從表格「用戶」那裏得到它們的時區,並「計算」適當的時間,我的代碼就是這樣的;
//Get correct destination time
DateTime thedate = DateTime.Now;
string destinationtimezone = null;
//Load the time zone where the file is going
TimeZoneInformation tzi = TimeZoneInformation.FromName(this.m_destinationtimezone);
//Calculate
destinationtimezone = tzi.FromUniversalTime(thedate.ToUniversalTime()).ToString();
此類具有Windows Vista中崩潰的問題「的fromIndex(INT指數)」的功能,但你可以修改代碼,而不是使用功能:
public static TimeZoneInformation FromIndex(int index)
{
TimeZoneInformation[] zones = EnumZones();
for (int i = 0; i < zones.Length; ++i)
{
if (zones[i].Index == index)
return zones[i];
}
throw new ArgumentOutOfRangeException("index", index, "Unknown time zone index");
}
你可以改變它至;
public static TimeZoneInformation FromName(string name)
{
TimeZoneInformation[] zones = EnumZones();
foreach (TimeZoneInformation tzi in zones)
{
if (tzi.DisplayName.Equals(name))
return tzi;
}
throw new ArgumentOutOfRangeException("name", name, "Unknown time zone name");
}