DateTime.ToUniversalTime DateTime.ToUniversalTime DateTime.ToUniversalTime DateTime.ToUniversalTime Method

定義

現在の DateTime オブジェクトの値を世界協定時刻 (UTC) に変換します。Converts the value of the current DateTime object to Coordinated Universal Time (UTC).

public:
 DateTime ToUniversalTime();
public DateTime ToUniversalTime ();
member this.ToUniversalTime : unit -> DateTime
Public Function ToUniversalTime () As DateTime

戻り値

Kind プロパティが Utc であるオブジェクト。通常は、現在の DateTime オブジェクトと等価な UTC が値として格納されます。ただし、変換後の値が MaxValue オブジェクトで表現可能な範囲を超えている場合は DateTime の値が格納され、逆に変換後の値が MinValue オブジェクトで表現可能な範囲を下回る場合は DateTime が格納されます。An object whose Kind property is Utc, and whose value is the UTC equivalent to the value of the current DateTime object, or MaxValue if the converted value is too large to be represented by a DateTime object, or MinValue if the converted value is too small to be represented by a DateTime object.

次の例で、ToUniversalTimeメソッド。The following example demonstrates the ToUniversalTime method.

using namespace System;

void main()
{
   Console::WriteLine("Enter a date and time.");
   String^ strDateTime = Console::ReadLine();

   DateTime localDateTime, univDateTime;
   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.
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.
Module Example
    Sub Main()
      Dim localDateTime, univDateTime As DateTime
      
      Console.WriteLine("Enter a date and time.")
      Dim strDateTime As String = Console.ReadLine()
      Try
         localDateTime = DateTime.Parse(strDateTime)
         univDateTime = localDateTime.ToUniversalTime()
         Console.WriteLine("{0} local time is {1} universal time.", 
                           localDateTime, univDateTime)
      Catch exp As FormatException
         Console.WriteLine("Invalid format.")
      End Try

      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 exp As FormatException
         Console.WriteLine("Invalid format.")
      End Try
    End Sub
End Module
' 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プロパティの影響、ToLocalTimeToUniversalTime変換メソッド。The following example uses the SpecifyKind method to demonstrate how the Kind property influences the ToLocalTime and ToUniversalTime conversion methods.

// 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

*/

' This code example demonstrates the DateTime Kind, Now, and
' UtcNow properties, and the SpecifyKind(), ToLocalTime(), 
' and ToUniversalTime() methods.
Imports System

Class Sample
    Public Shared Sub Main() 
        ' Get the date and time for the current moment, adjusted 
        ' to the local time zone.
        Dim saveNow As DateTime = DateTime.Now
        
        ' Get the date and time for the current moment expressed 
        ' as coordinated universal time (UTC).
        Dim saveUtcNow As DateTime = DateTime.UtcNow
        Dim myDt As DateTime
        
        ' 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)
    End Sub 'Main
    
    ' 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 Shared datePatt As String = "M/d/yyyy hh:mm:ss tt"
    
    Public Shared Sub Display(ByVal title As String, ByVal inputDt As DateTime) 
        Dim dispDt As DateTime = inputDt
        Dim dtString As String
        
        ' 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()
    End Sub 'Display
    
    
    ' Display the value and Kind property for DateTime.Now and DateTime.UtcNow.

    Public Shared Sub DisplayNow(ByVal title As String, ByVal inputDt As DateTime) 
        Dim dtString As String = inputDt.ToString(datePatt)
        Console.WriteLine("{0} {1}, Kind = {2}", title, dtString, inputDt.Kind)
    End Sub 'DisplayNow
End Class 'Sample

'
'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 オフセットを引いたのローカル時刻です。The Coordinated Universal Time (UTC) is equal to the local time minus the UTC offset. UTC オフセットの詳細については、次を参照してください。TimeZone.GetUtcOffsetします。For more information about the UTC offset, see TimeZone.GetUtcOffset. 変換も考慮現在によって表される時刻に適用される夏時間規則DateTimeオブジェクト。The conversion also takes into account the daylight saving time rule that applies to the time represented by the current DateTime object.

重要

Windows XPWindows XP 、システム、ToUniversalTimeメソッドは、現地時刻から UTC に変換するときに、現在の調整規則のみを認識します。On Windows XPWindows XP systems, the ToUniversalTime method recognizes only the current adjustment rule when converting from local time to UTC. その結果、現在の調整規則を有効にされる前に、の期間を変換すると、ローカル時刻と UTC の間の差が正確には反映されない可能性があります。As a result, conversions for periods before the current adjustment rule came into effect may not accurately reflect the difference between local time and UTC.

.NET Framework version 2.0 では、によって返される値で始まる、ToUniversalTimeメソッドはによって決定されます、Kindプロパティ、現在のDateTimeオブジェクト。Starting with the .NET Framework version 2.0, the value returned by the ToUniversalTime method is determined by the Kind property of the current DateTime object. 次の表では、考えられる結果について説明します。The following table describes the possible results.

種類Kind 結果Results
Utc 変換は行われません。No conversion is performed.
Local 現在DateTimeオブジェクトは UTC に変換します。The current DateTime object is converted to UTC.
Unspecified 現在DateTimeオブジェクトは、現地時刻と見なされ、変換を実行まるでKindされたLocalします。The current DateTime object is assumed to be a local time, and the conversion is performed as if Kind were Local.

注意

ToUniversalTimeメソッドに変換するDateTime値を現地時刻から UTC にします。The ToUniversalTime method converts a DateTime value from local time to UTC. UTC にローカル以外のタイム ゾーンの時刻に変換する、TimeZoneInfo.ConvertTimeToUtc(DateTime, TimeZoneInfo)メソッド。To convert the time in a non-local time zone to UTC, use the TimeZoneInfo.ConvertTimeToUtc(DateTime, TimeZoneInfo) method. UTC からオフセットが呼ばれる時刻に変換する、ToUniversalTimeメソッド。To convert a time whose offset from UTC is known, use the ToUniversalTime method.

インスタンスの日付と時刻の値があいまいな時刻の場合は、このメソッドは、標準時であると仮定します。If the date and time instance value is an ambiguous time, this method assumes that it is a standard time. (あいまいな時刻は、標準時またはローカル タイム ゾーンの夏時間のいずれかにマップできる)インスタンスの日付と時刻の値が無効な時刻の場合は、このメソッドは単に返す UTC オフセットのローカル タイム ゾーンの UTC から現地時刻を減算します。(An ambiguous time is one that can map either to a standard time or to a daylight saving time in the local time zone) If the date and time instance value is an invalid time, this method simply subtracts the local time from the local time zone's UTC offset to return UTC. (無効な時刻は夏時間調整規則の適用のために存在しません。)(An invalid time is one that does not exist because of the application of daylight saving time adjustment rules.)

注意 (呼び出し元)

ToUniversalTime()現地時刻を UTC に変換するメソッドが使用される場合があります。The ToUniversalTime() method is sometimes used to convert a local time to UTC. ToLocalTime()復元元のローカル時間にはメソッドが呼び出されます。The ToLocalTime() method is then called to restore the original local time. ただし、ローカル タイム ゾーンを元の時刻が無効な時刻を表している場合、2 つのローカル時刻の値はいないと等しいします。However, if the original time represents an invalid time in the local time zone, the two local time values will not be equal. 追加情報と例では、次を参照してください。、ToLocalTime()メソッド。For additional information and an example, see the ToLocalTime() method.

Windows XPWindows XPシステムでは、ToUniversalTime()メソッドは、下位レベルの日付 (つまり、日付は現在の調整の開始日より前のなどのすべての日付に適用するローカル タイム ゾーンの現在の調整規則のみを認識します。ルールの場合)。On Windows XPWindows XP systems, the ToUniversalTime() method recognizes only the current adjustment rule for the local time zone, which it applies to all dates, including down-level dates (that is, dates that are earlier than the starting date of the current adjustment rule). アプリケーションで実行されているWindows XPWindows XPを必要とする正確なローカルの日付と時刻の計算を使用してこの問題を回避する必要があります、FindSystemTimeZoneById(String)を取得するメソッド、TimeZoneInfoローカル タイム ゾーンに対応するオブジェクトし、呼び出しそのConvertTimeToUtc(DateTime, TimeZoneInfo)メソッド。Applications running on Windows XPWindows XP that require historically accurate local date and time calculations must work around this behavior by using the FindSystemTimeZoneById(String) method to retrieve a TimeZoneInfo object that corresponds to the local time zone and calling its ConvertTimeToUtc(DateTime, TimeZoneInfo) method.

次の例では、違いを示しています、ToUniversalTime()ConvertTimeToUtc(DateTime, TimeZoneInfo)メソッド、Windows XPWindows XP米国内のシステム表示します。The following example illustrates the difference between the ToUniversalTime() and ConvertTimeToUtc(DateTime, TimeZoneInfo) methods on a Windows XPWindows XP system in the U.S. Pacific Time zone. 最初の 2 つのメソッド呼び出しでは、2006 年の日付に (2007 年には、有効に問題が発生しました) を現在のタイム ゾーン調整規則が適用されます。The first two method calls apply the current time zone adjustment rule (which went into effect in 2007) to a date in 2006. 現在の調整規則; 3 月の第 2 日曜日夏時間に移行しています2006 年 4 月の最初の日曜日に発生する夏時間への移行に対して提供に有効であった前ルール。The current adjustment rule provides for the transition to daylight saving time on the second Sunday of March; the previous rule, which was in effect in 2006, provided for the transition to daylight saving time to occur on the first Sunday of April. この古代の日付と時刻の変換を正確に 3 つ目のメソッドの呼び出しのみ実行します。Only the third method call accurately performs this historical date and time conversion.

[!code-csharpSystem.DateTime.ToUniversalTime#1] [!code-vbSystem.DateTime.ToUniversalTime#1][!code-csharpSystem.DateTime.ToUniversalTime#1] [!code-vbSystem.DateTime.ToUniversalTime#1]

適用対象

こちらもご覧ください