Note: Different OS will have different Time Zone Id.

On Windows systems, FindSystemTimeZoneById tries to match id to the subkey names of the HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Time Zones branch of the registry. On Linux and macOS, it uses time zone information available in the ICU Library

Windows OS

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

Non Windows OS

America/Los_Angeles
public static DateTime GetPacificStandardTime()
{
    var utc = DateTime.UtcNow;
    TimeZoneInfo pacificZone = TimeZoneInfo.FindSystemTimeZoneById("America/Los_Angeles");
    var pacificTime = TimeZoneInfo.ConvertTimeFromUtc(utc, pacificZone);
    return pacificTime;
}

Time Zone List

https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

CodeUTCWindows OSNon Windows OS
PSTUTC-8Pacific Standard TimeAmerica/Los_Angeles
MSTUTC-7Mountain Standard TimeAmerica/Denver
CSTUTC-6Central Standard TimeAmerica/Chicago
ESTUTC-5Eastern Standard TimeAmerica/New_York

Sources:

https://stackoverflow.com/questions/8862335/convert-local-time-zone-to-pst-time-zone-in-c-sharp

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

https://docs.microsoft.com/en-us/dotnet/api/system.timezoneinfo.findsystemtimezonebyid?view=net-5.0

Last modified: May 29, 2021

Author

Comments

Write a Reply or Comment