Maybe it will sound a bit weird, but i actually know a solution for this problem. My initial solution didn't work and i actually want to why. It is an Asp.Net MVC application.
This didn't work:
public static DateTime ConvertToUTC(String userTimeZone, DateTime date)
{
var result = TimeZoneInfo.ConvertTimeToUtc(date, TimeZoneInfo.FindSystemTimeZoneById(userTimeZone));
return result;
}
public static DateTime ConvertToUserTime(string userTimeZone, DateTime date)
{
var result = TimeZoneInfo.ConvertTimeFromUtc(date, TimeZoneInfo.FindSystemTimeZoneById(userTimeZone));
return result;
}
I couldn't convert the time back to it's original time.
This did work:
public DateTime ConvertToUTC(String userTimeZone, DateTime date)
{
var result = TimeZoneInfo.ConvertTimeToUtc(result, TimeZoneInfo.FindSystemTimeZoneById(userTimeZone));
return result;
}
public DateTime ConvertToUserTime(string userTimeZone, DateTime date)
{
var userTimezoneInfo = TimeZoneInfo.FindSystemTimeZoneById(userTimeZone);
var result = date.Add(userTimezoneInfo.BaseUtcOffset);
return result;
}
Does anybody have an idea?
对于此问题,.NET 3.5中引入了特殊的DataType: DateTimeOffset,其功能与DateTime类似,但它还存储UTC偏移量。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.