次の方法で共有


DateTimeOffset.ToUniversalTime メソッド

定義

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

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

戻り値

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

次の例では、ToUniversalTime メソッドを呼び出して、現地時刻およびその他のいくつかの時刻を協定世界時 (UTC) に変換します。

DateTimeOffset localTime, otherTime, universalTime;

// Define local time in local time zone
localTime = new DateTimeOffset(new DateTime(2007, 6, 15, 12, 0, 0));
Console.WriteLine("Local time: {0}", localTime);
Console.WriteLine();

// Convert local time to offset 0 and assign to otherTime
otherTime = localTime.ToOffset(TimeSpan.Zero);
Console.WriteLine("Other time: {0}", otherTime);
Console.WriteLine("{0} = {1}: {2}",
                  localTime, otherTime,
                  localTime.Equals(otherTime));
Console.WriteLine("{0} exactly equals {1}: {2}",
                  localTime, otherTime,
                  localTime.EqualsExact(otherTime));
Console.WriteLine();

// Convert other time to UTC
universalTime = localTime.ToUniversalTime();
Console.WriteLine("Universal time: {0}", universalTime);
Console.WriteLine("{0} = {1}: {2}",
                  otherTime, universalTime,
                  universalTime.Equals(otherTime));
Console.WriteLine("{0} exactly equals {1}: {2}",
                  otherTime, universalTime,
                  universalTime.EqualsExact(otherTime));
Console.WriteLine();
// The example produces the following output to the console:
//    Local time: 6/15/2007 12:00:00 PM -07:00
//
//    Other time: 6/15/2007 7:00:00 PM +00:00
//    6/15/2007 12:00:00 PM -07:00 = 6/15/2007 7:00:00 PM +00:00: True
//    6/15/2007 12:00:00 PM -07:00 exactly equals 6/15/2007 7:00:00 PM +00:00: False
//
//    Universal time: 6/15/2007 7:00:00 PM +00:00
//    6/15/2007 7:00:00 PM +00:00 = 6/15/2007 7:00:00 PM +00:00: True
//    6/15/2007 7:00:00 PM +00:00 exactly equals 6/15/2007 7:00:00 PM +00:00: True
// Define local time in local time zone
let localTime = DateTimeOffset(DateTime(2007, 6, 15, 12, 0, 0))
printfn $"Local time: {localTime}\n"

// Convert local time to offset 0 and assign to otherTime
let otherTime = localTime.ToOffset TimeSpan.Zero
printfn $"Other time: {otherTime}"
printfn $"{localTime} = {otherTime}: {localTime.Equals otherTime}"                    
printfn $"{localTime} exactly equals {otherTime}: {localTime.EqualsExact otherTime}\n"

// Convert other time to UTC
let universalTime = localTime.ToUniversalTime()
printfn $"Universal time: {universalTime}"
printfn $"{otherTime} = {universalTime}: {universalTime.Equals otherTime}"                    
printfn $"{otherTime} exactly equals {universalTime}: {universalTime.EqualsExact otherTime}\n"

// The example produces the following output to the console:
//    Local time: 6/15/2007 12:00:00 PM -07:00
//
//    Other time: 6/15/2007 7:00:00 PM +00:00
//    6/15/2007 12:00:00 PM -07:00 = 6/15/2007 7:00:00 PM +00:00: True
//    6/15/2007 12:00:00 PM -07:00 exactly equals 6/15/2007 7:00:00 PM +00:00: False
//
//    Universal time: 6/15/2007 7:00:00 PM +00:00
//    6/15/2007 7:00:00 PM +00:00 = 6/15/2007 7:00:00 PM +00:00: True
//    6/15/2007 7:00:00 PM +00:00 exactly equals 6/15/2007 7:00:00 PM +00:00: True
Dim localTime, otherTime, universalTime As DateTimeOffset
 
' Define local time in local time zone
localTime = New DateTimeOffset(#6/15/2007 12:00:00PM#)
Console.WriteLine("Local time: {0}", localTime)
Console.WriteLine()

' Convert local time to offset 0 and assign to otherTime
otherTime = localTime.ToOffset(TimeSpan.Zero)
Console.WriteLine("Other time: {0}", otherTime)
Console.WriteLine("{0} = {1}: {2}", _
                  localTime, otherTime, _
                  localTime.Equals(otherTime))
Console.WriteLine("{0} exactly equals {1}: {2}", _ 
                  localTime, otherTime, _
                  localTime.EqualsExact(otherTime))
Console.WriteLine()
                  
' Convert other time to UTC
universalTime = localTime.ToUniversalTime() 
Console.WriteLine("Universal time: {0}", universalTime)
Console.WriteLine("{0} = {1}: {2}", _
                  otherTime, universalTime, _ 
                  universalTime.Equals(otherTime))
Console.WriteLine("{0} exactly equals {1}: {2}", _ 
                  otherTime, universalTime, _
                  universalTime.EqualsExact(otherTime))
Console.WriteLine()
' The example produces the following output to the console:
'    Local time: 6/15/2007 12:00:00 PM -07:00
'    
'    Other time: 6/15/2007 7:00:00 PM +00:00
'    6/15/2007 12:00:00 PM -07:00 = 6/15/2007 7:00:00 PM +00:00: True
'    6/15/2007 12:00:00 PM -07:00 exactly equals 6/15/2007 7:00:00 PM +00:00: False
'    
'    Universal time: 6/15/2007 7:00:00 PM +00:00
'    6/15/2007 7:00:00 PM +00:00 = 6/15/2007 7:00:00 PM +00:00: True
'    6/15/2007 7:00:00 PM +00:00 exactly equals 6/15/2007 7:00:00 PM +00:00: True

注釈

このメソッドは、プロパティが DateTimeOffset 0 に設定されているオブジェクトを返します Offset

現在 DateTimeOffset のオブジェクトの値と、メソッド呼び出しによって返されるオブジェクトの値の DateTimeOffset 両方が、同じ時点を表します。 つまり、両方が メソッドに DateTimeOffset.Equals(DateTimeOffset, DateTimeOffset) 渡された場合、メソッドは を返します true

適用対象

こちらもご覧ください