Convert Local Time Zone to PST Time Zone in C#

限于喜欢 提交于 2019-12-22 05:18:18

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!