问题
Let say my time zone in system right now is +5GMT
right now on my machine 01/14/2012 05:52PM I want to convert it into PST time zone like
1/14/12 4:52:50 AM PST
and vice versa PST to GMT
回答1:
TimeZoneInfo timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Pacific SA Standard Time");
DateTime newDateTime = TimeZoneInfo.ConvertTime(existingDateTime, timeZoneInfo);
You can see complete chart of available Time Zones here
Also take a look at Converting Between Any Two Time Zones
回答2:
Inspired by @HarisHasan's answer above, the following method will produce PST no matter where your code is running:
public static DateTime GetPacificStandardTime()
{
var utc = DateTime.UtcNow;
TimeZoneInfo pacificZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var pacificTime = TimeZoneInfo.ConvertTimeFromUtc(utc, pacificZone);
return pacificTime;
}
回答3:
Below code convert to PST.
TimeZoneInfo timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
来源:https://stackoverflow.com/questions/8862335/convert-local-time-zone-to-pst-time-zone-in-c-sharp