C# タイムゾーンによる日時変換(UTC->JST, JST->UTC)

日本時間(JST)を世界標準時間(UTC)へ変換したり、その反対の変換を行うためにはタイムゾーンによる変換方法を調べて試しました。

タイムゾーンは日本時間の場合は “Tokyo Standard Time” で TimeZoneInfo が取得できます。参考までに他のタイムゾーンも列挙してみました。

using System;

namespace TZTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string date = "2013-02-07 12:13:00";   // テスト用の日時
            DateTime jst = DateTime.Parse(date);    // ローカル日時
            DateTime utc = jst.ToUniversalTime();   // UTC へ変換
            Console.WriteLine("JST: " + jst.ToString("G") + " -> UTC: " + utc.ToString("G"));

            // タイムゾーンを指定してUTCからJSTへ変換
            TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("Tokyo Standard Time");
            DateTime jst2 = TimeZoneInfo.ConvertTimeFromUtc(utc, tzi);
            Console.WriteLine("UTC: " + utc.ToString("G") + " -> JST: " + jst2.ToString("G"));

            // タイムゾーンIDの列挙
            var infos = TimeZoneInfo.GetSystemTimeZones();
            foreach (var info in infos)
            {
                Console.WriteLine(info.Id);
            }        
        }
    }
}

出力結果:

JST: 2013/02/07 12:13:00 -> UTC: 2013/02/07 3:13:00
UTC: 2013/02/07 3:13:00 -> JST: 2013/02/07 12:13:00

Dateline Standard Time
UTC-11
Hawaiian Standard Time
Alaskan Standard Time
Pacific Standard Time (Mexico)
Pacific Standard Time
US Mountain Standard Time
Mountain Standard Time (Mexico)
Mexico Standard Time 2
Mountain Standard Time
Central Standard Time (Mexico)
Mexico Standard Time
Canada Central Standard Time
Central America Standard Time
Central Standard Time
US Eastern Standard Time
SA Pacific Standard Time
Eastern Standard Time
Venezuela Standard Time
Paraguay Standard Time
Central Brazilian Standard Time
Pacific SA Standard Time
SA Western Standard Time
Atlantic Standard Time
Newfoundland Standard Time
SA Eastern Standard Time
Greenland Standard Time
Bahia Standard Time
Argentina Standard Time
E. South America Standard Time
Montevideo Standard Time
Mid-Atlantic Standard Time
UTC-02
Azores Standard Time
Cape Verde Standard Time
Morocco Standard Time
GMT Standard Time
Greenwich Standard Time
UTC
W. Europe Standard Time
Namibia Standard Time
Central European Standard Time
Romance Standard Time
Central Europe Standard Time
W. Central Africa Standard Time
GTB Standard Time
Turkey Standard Time
Israel Standard Time
Egypt Standard Time
Syria Standard Time
South Africa Standard Time
FLE Standard Time
Middle East Standard Time
E. Europe Standard Time
Jordan Standard Time
Kaliningrad Standard Time
Arab Standard Time
E. Africa Standard Time
Arabic Standard Time
Iran Standard Time
Arabian Standard Time
Armenian Standard Time
Caucasus Standard Time
Georgian Standard Time
Azerbaijan Standard Time
Mauritius Standard Time
Russian Standard Time
Afghanistan Standard Time
Pakistan Standard Time
West Asia Standard Time
Sri Lanka Standard Time
India Standard Time
Nepal Standard Time
Central Asia Standard Time
Ekaterinburg Standard Time
Bangladesh Standard Time
Myanmar Standard Time
N. Central Asia Standard Time
SE Asia Standard Time
Ulaanbaatar Standard Time
Singapore Standard Time
North Asia Standard Time
W. Australia Standard Time
China Standard Time
Taipei Standard Time
North Asia East Standard Time
Korea Standard Time
Tokyo Standard Time
Cen. Australia Standard Time
AUS Central Standard Time
AUS Eastern Standard Time
West Pacific Standard Time
E. Australia Standard Time
Tasmania Standard Time
Yakutsk Standard Time
Vladivostok Standard Time
Central Pacific Standard Time
New Zealand Standard Time
Fiji Standard Time
Kamchatka Standard Time
Magadan Standard Time
UTC+12
Samoa Standard Time
Tonga Standard Time

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください