# conversion time ###### tags: `C#` ```csharp var easternZoneId = "Singapore Standard Time"; //設定 TimeZoneInfo TimeZoneInfo easternZone = TimeZoneInfo.FindSystemTimeZoneById(easternZoneId); //換成 UTC TimeZoneInfo.ConvertTimeToUtc(easternTime, easternZone); //用系統的 timeinfo 轉換成 UTC TimeZoneInfo.ConvertTimeToUtc(dateNow); //將 utc 轉換成指定的 timezone TimeZoneInfo.ConvertTimeFromUtc(timeUtc, easternZone); ``` 各個 Id 名稱所對應的 timezone,可以從[這邊](https://stackoverflow.com/a/17300423/19304838)查 [Converting times between time zones 官方文件](https://learn.microsoft.com/en-us/dotnet/standard/datetime/converting-between-time-zones) 可能因為作業系統不同出現錯誤訊息 `System.IO.FileNotFoundException : Could not find file '/usr/share/zoneinfo/Singapore Standard Time'.`,這時就需要判斷作業系統給不同的設定 Linux 對應的 Id 可以[這裡查詢](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) ```csharp public static TimeZoneInfo GetSingaporeZone() { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { return TimeZoneInfo.FindSystemTimeZoneById("Singapore Standard Time"); } if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { return TimeZoneInfo.FindSystemTimeZoneById("Asia/Singapore"); } if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { return TimeZoneInfo.FindSystemTimeZoneById("Asia/Singapore"); } return null; } ``` [TimeZoneInfo in .NET Core when hosting on unix (nginx)](https://stackoverflow.com/a/55511180/19304838)