English 中文(简体)
C# 将当地时间区改为PST时区
原标题:Convert Local Time Zone to PST Time Zone in C#
  • 时间:2012-01-14 12:54:37
  •  标签:
  • c#
  • timezone

请允许我说,现在,我的系统时区是+5GMT。

右上机01/14/2012 05:52PM/code> 我想把它变成像PST时代一样的平时区。

1/14/12 4:52:50 AM PST

反之,PST改为GMT

最佳回答
TimeZoneInfo timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Pacific SA Standard Time");

DateTime newDateTime = TimeZoneInfo.ConvertTime(existingDateTime, timeZoneInfo);

http://www.xiirus.net/articles/article-_net-convert-datetime- from-one-timezone-to-another-7e44y.aspx>here

Also take a look at Converting Between Any Two Time Zones

问题回答

在上述“HarisHasan”答复的启发下,以下方法将产生《标准指示》,不管贵国的法规如何运作:

    public static DateTime GetPacificStandardTime()
    {
        var utc = DateTime.UtcNow;
        TimeZoneInfo pacificZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
        var pacificTime = TimeZoneInfo.ConvertTimeFromUtc(utc, pacificZone);
        return pacificTime;
    }
TimeZoneInfo.FindSystemTimeZoneById("Pacific SA Standard Time");

Will fail if we deploy the application in Linux (In case of .NET Core), found this nice library which will give correct TimeZoneInfo in windows and Linux "TimeZoneConverter", using this we can convert like

using TimeZoneConverter;

TimeZoneInfo pst =TZConvert.GetTimeZoneInfo("Pacific Standard Time");
DateTime newDateTime = TimeZoneInfo.ConvertTime(existingDateTime, pst);





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...