英語で読む

次の方法で共有


DateTime.ToUniversalTime メソッド

定義

現在の DateTime オブジェクトの値を世界協定時刻 (UTC) に変換します。

C#
public DateTime ToUniversalTime();

戻り値

プロパティが KindUtc、値が現在DateTimeのオブジェクトの値に相当する UTC であるオブジェクト。変換後の値が大きすぎてオブジェクトでDateTime表すには DateTime.MaxValue、変換後の値が小さすぎてオブジェクトでDateTime表せなければ DateTime.MinValue

ToUniversalTimeメソッドの例を次に示します。

C#
using System;

class Example
{
    static void Main()
    {
        DateTime localDateTime, univDateTime;
        
        Console.WriteLine("Enter a date and time.");
        string strDateTime = Console.ReadLine();

        try {
            localDateTime = DateTime.Parse(strDateTime);
            univDateTime = localDateTime.ToUniversalTime();

            Console.WriteLine("{0} local time is {1} universal time.",
                                localDateTime,
                                    univDateTime);
        }
        catch (FormatException) {
            Console.WriteLine("Invalid format.");
            return;
        }

        Console.WriteLine("Enter a date and time in universal time.");
        strDateTime = Console.ReadLine();

        try {
            univDateTime = DateTime.Parse(strDateTime);
            localDateTime = univDateTime.ToLocalTime();

            Console.WriteLine("{0} universal time is {1} local time.",
                                     univDateTime,
                                     localDateTime);
        }
        catch (FormatException) {
            Console.WriteLine("Invalid format.");
            return;
        }
    }
}
// The example displays output like the following when run on a
// computer whose culture is en-US in the Pacific Standard Time zone:
//     Enter a date and time.
//     12/10/2015 6:18 AM
//     12/10/2015 6:18:00 AM local time is 12/10/2015 2:18:00 PM universal time.
//     Enter a date and time in universal time.
//     12/20/2015 6:42:00
//     12/20/2015 6:42:00 AM universal time is 12/19/2015 10:42:00 PM local time.

次の例では、 メソッドをSpecifyKind使用して、 プロパティが Kind および ToUniversalTime 変換メソッドにどのように影響ToLocalTimeするかを示します。

C#
// This code example demonstrates the DateTime Kind, Now, and
// UtcNow properties, and the SpecifyKind(), ToLocalTime(),
// and ToUniversalTime() methods.

using System;

class Sample
{
    public static void Main()
    {
        // Get the date and time for the current moment, adjusted
        // to the local time zone.

        DateTime saveNow = DateTime.Now;

        // Get the date and time for the current moment expressed
        // as coordinated universal time (UTC).

        DateTime saveUtcNow = DateTime.UtcNow;
        DateTime myDt;

        // Display the value and Kind property of the current moment
        // expressed as UTC and local time.

        DisplayNow("UtcNow: ..........", saveUtcNow);
        DisplayNow("Now: .............", saveNow);
        Console.WriteLine();

        // Change the Kind property of the current moment to
        // DateTimeKind.Utc and display the result.

        myDt = DateTime.SpecifyKind(saveNow, DateTimeKind.Utc);
        Display("Utc: .............", myDt);

        // Change the Kind property of the current moment to
        // DateTimeKind.Local and display the result.

        myDt = DateTime.SpecifyKind(saveNow, DateTimeKind.Local);
        Display("Local: ...........", myDt);

        // Change the Kind property of the current moment to
        // DateTimeKind.Unspecified and display the result.

        myDt = DateTime.SpecifyKind(saveNow, DateTimeKind.Unspecified);
        Display("Unspecified: .....", myDt);
    }

    // Display the value and Kind property of a DateTime structure, the
    // DateTime structure converted to local time, and the DateTime
    // structure converted to universal time.

    public static string datePatt = @"M/d/yyyy hh:mm:ss tt";
    public static void Display(string title, DateTime inputDt)
    {
        DateTime dispDt = inputDt;
        string dtString;

        // Display the original DateTime.

        dtString = dispDt.ToString(datePatt);
        Console.WriteLine("{0} {1}, Kind = {2}",
                          title, dtString, dispDt.Kind);

        // Convert inputDt to local time and display the result.
        // If inputDt.Kind is DateTimeKind.Utc, the conversion is performed.
        // If inputDt.Kind is DateTimeKind.Local, the conversion is not performed.
        // If inputDt.Kind is DateTimeKind.Unspecified, the conversion is
        // performed as if inputDt was universal time.

        dispDt = inputDt.ToLocalTime();
        dtString = dispDt.ToString(datePatt);
        Console.WriteLine("  ToLocalTime:     {0}, Kind = {1}",
                          dtString, dispDt.Kind);

        // Convert inputDt to universal time and display the result.
        // If inputDt.Kind is DateTimeKind.Utc, the conversion is not performed.
        // If inputDt.Kind is DateTimeKind.Local, the conversion is performed.
        // If inputDt.Kind is DateTimeKind.Unspecified, the conversion is
        // performed as if inputDt was local time.

        dispDt = inputDt.ToUniversalTime();
        dtString = dispDt.ToString(datePatt);
        Console.WriteLine("  ToUniversalTime: {0}, Kind = {1}",
                          dtString, dispDt.Kind);
        Console.WriteLine();
    }

    // Display the value and Kind property for DateTime.Now and DateTime.UtcNow.

    public static void DisplayNow(string title, DateTime inputDt)
    {
        string dtString = inputDt.ToString(datePatt);
        Console.WriteLine("{0} {1}, Kind = {2}",
                          title, dtString, inputDt.Kind);
    }
}

/*
This code example produces the following results:

UtcNow: .......... 5/6/2005 09:34:42 PM, Kind = Utc
Now: ............. 5/6/2005 02:34:42 PM, Kind = Local

Utc: ............. 5/6/2005 02:34:42 PM, Kind = Utc
  ToLocalTime:     5/6/2005 07:34:42 AM, Kind = Local
  ToUniversalTime: 5/6/2005 02:34:42 PM, Kind = Utc

Local: ........... 5/6/2005 02:34:42 PM, Kind = Local
  ToLocalTime:     5/6/2005 02:34:42 PM, Kind = Local
  ToUniversalTime: 5/6/2005 09:34:42 PM, Kind = Utc

Unspecified: ..... 5/6/2005 02:34:42 PM, Kind = Unspecified
  ToLocalTime:     5/6/2005 07:34:42 AM, Kind = Local
  ToUniversalTime: 5/6/2005 09:34:42 PM, Kind = Utc

*/

注釈

協定世界時 (UTC) は、現地時刻から UTC オフセットを引いた値に等しくなります。 UTC オフセットの詳細については、「」を参照してください TimeZoneInfo.GetUtcOffset。 変換では、現在 DateTime の オブジェクトによって表される時刻に適用される夏時間ルールも考慮されます。

重要

Windows XP システムでは、 メソッドは ToUniversalTime ローカル時刻から UTC に変換するときに、現在の調整規則のみを認識します。 その結果、現在の調整規則が有効になる前の期間の変換は、現地時刻と UTC の違いを正確に反映していない可能性があります。

.NET Framework バージョン 2.0 以降では、 メソッドによってToUniversalTime返される値は、現在DateTimeの オブジェクトの プロパティによってKind決まります。 次の表では、考えられる結果について説明します。

種類 結果
Utc 変換は実行されません。
Local 現在の DateTime オブジェクトは UTC に変換されます。
Unspecified 現在の DateTime オブジェクトはローカル時刻であると見なされ、 のように変換が実行KindLocalされます。

注意

メソッドは ToUniversalTime 、値を DateTime 現地時刻から UTC に変換します。 ローカル以外のタイム ゾーンの時刻を UTC に変換するには、 メソッドを使用します TimeZoneInfo.ConvertTimeToUtc(DateTime, TimeZoneInfo) 。 UTC からのオフセットがわかっている時刻を変換するには、 メソッドを使用します ToUniversalTime

日付と時刻のインスタンス値があいまいな時刻の場合、このメソッドは標準時刻であると見なします。 (あいまいな時刻は、標準時またはローカル タイム ゾーンの夏時間にマップできる時刻です)日付と時刻のインスタンス値が無効な時刻の場合、このメソッドは単にローカル タイム ゾーンの UTC オフセットからローカル時刻を減算して UTC を返します。 (無効な時刻は、夏時間調整規則の適用により存在しない時刻です)。

注意 (呼び出し元)

メソッドは ToUniversalTime() 、ローカル時刻を UTC に変換するために使用される場合があります。 ToLocalTime()その後、 メソッドが呼び出され、元のローカル時刻が復元されます。 ただし、元の時刻がローカル タイム ゾーンの無効な時刻を表している場合、2 つのローカル時刻の値は等しくありません。 詳細と例については、 メソッドを ToLocalTime() 参照してください。

Windows XP システムでは、 ToUniversalTime() メソッドはローカル タイム ゾーンの現在の調整規則のみを認識します。これは、下位の日付 (つまり、現在の調整規則の開始日より前の日付) を含むすべての日付に適用されます。 過去に正確なローカルの日付と時刻の計算を必要とする Windows XP で実行されているアプリケーションでは、 メソッドを FindSystemTimeZoneById(String) 使用してローカル タイム ゾーンに対応するオブジェクトを取得 TimeZoneInfo し、その ConvertTimeToUtc(DateTime, TimeZoneInfo) メソッドを呼び出すことで、この動作を回避する必要があります。

次の例は、米国太平洋タイム ゾーンの Windows XP システムの メソッドと ConvertTimeToUtc(DateTime, TimeZoneInfo) メソッドの違いToUniversalTime()を示しています。 最初の 2 つのメソッド呼び出しでは、現在のタイム ゾーン調整規則 (2007 年に有効) が 2006 年の日付に適用されます。 現在の調整規則では、3 月の第 2 日曜日に夏時間への移行が提供されます。2006 年に有効だった前の規則は、夏時間への移行が 4 月の第 1 日曜日に発生するために提供されました。 3 番目のメソッド呼び出しだけが、この履歴の日付と時刻の変換を正確に実行します。

C#
using System;

public class Example
{
   public static void Main()
   {
      DateTime date1 = new DateTime(2006, 3, 21, 2, 0, 0);

      Console.WriteLine(date1.ToUniversalTime());
      Console.WriteLine(TimeZoneInfo.ConvertTimeToUtc(date1));

      TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
      Console.WriteLine(TimeZoneInfo.ConvertTimeToUtc(date1, tz));
   }
}
// The example displays the following output on Windows XP systems:
//       3/21/2006 9:00:00 AM
//       3/21/2006 9:00:00 AM
//       3/21/2006 10:00:00 AM

適用対象

製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

こちらもご覧ください