Convert Local Time Zone to PST Time Zone in C#

2019-04-29 07:30发布

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

标签: c# timezone
3条回答
2楼-- · 2019-04-29 07:49

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楼-- · 2019-04-29 08:01
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

查看更多
SAY GOODBYE
4楼-- · 2019-04-29 08:02

Below code convert to PST.

TimeZoneInfo timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
查看更多
登录 后发表回答