你可以用这篇优秀的文章“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");
}