Partager via


DateTimeOffset.ToUniversalTime Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Converts the current DateTimeOffset object to a DateTimeOffset value that represents the Coordinated Universal Time (UTC).

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Function ToUniversalTime As DateTimeOffset
public DateTimeOffset ToUniversalTime()

Return Value

Type: System.DateTimeOffset
An object that represents the date and time of the current DateTimeOffset object converted to Coordinated Universal Time (UTC).

Remarks

This method returns a DateTimeOffset object whose Offset property is set to zero.

Both the value of the current DateTimeOffset object and the value of the DateTimeOffset object returned by the method call represent the same point in time. That is, if both are passed to the DateTimeOffset.Equals(DateTimeOffset, DateTimeOffset) method, the method will return true.

Examples

The following example calls the ToUniversalTime method to convert a local time and several other times to Coordinated Universal Time (UTC).

Dim localTime, otherTime, universalTime As DateTimeOffset

' Define local time in local time zone
localTime = New DateTimeOffset(#6/15/2007 12:00:00 PM#)
outputBlock.Text += String.Format("Local time: {0}", localTime) & vbCrLf
outputBlock.Text &= vbCrLf

' Convert local time to offset 0 and assign to otherTime
otherTime = localTime.ToOffset(TimeSpan.Zero)
outputBlock.Text += String.Format("Other time: {0}", otherTime) & vbCrLf
outputBlock.Text += String.Format("{0} = {1}: {2}", _
                  localTime, otherTime, _
                  localTime.Equals(otherTime)) + vbCrLf
outputBlock.Text += String.Format("{0} exactly equals {1}: {2}", _
                  localTime, otherTime, _
                  localTime.EqualsExact(otherTime)) + vbCrLf
outputBlock.Text &= vbCrLf

' Convert other time to UTC
universalTime = localTime.ToUniversalTime()
outputBlock.Text += String.Format("Universal time: {0}", universalTime) & vbCrLf
outputBlock.Text += String.Format("{0} = {1}: {2}", _
                  otherTime, universalTime, _
                  universalTime.Equals(otherTime)) + vbCrLf
outputBlock.Text += String.Format("{0} exactly equals {1}: {2}", _
                  otherTime, universalTime, _
                  universalTime.EqualsExact(otherTime)) + vbCrLf
outputBlock.Text &= vbCrLf
' The example produces the following output:
'    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 localTime, otherTime, universalTime;

// Define local time in local time zone
localTime = new DateTimeOffset(new DateTime(2007, 6, 15, 12, 0, 0));
outputBlock.Text += String.Format("Local time: {0}", localTime) + "\n";
outputBlock.Text += "\n";

// Convert local time to offset 0 and assign to otherTime
otherTime = localTime.ToOffset(TimeSpan.Zero);
outputBlock.Text += String.Format("Other time: {0}", otherTime) + "\n";
outputBlock.Text += String.Format("{0} = {1}: {2}",
                  localTime, otherTime,
                  localTime.Equals(otherTime)) + "\n";
outputBlock.Text += String.Format("{0} exactly equals {1}: {2}",
                  localTime, otherTime,
                  localTime.EqualsExact(otherTime)) + "\n";
outputBlock.Text += "\n";

// Convert other time to UTC
universalTime = localTime.ToUniversalTime();
outputBlock.Text += String.Format("Universal time: {0}", universalTime) + "\n";
outputBlock.Text += String.Format("{0} = {1}: {2}",
                  otherTime, universalTime,
                  universalTime.Equals(otherTime)) + "\n";
outputBlock.Text += String.Format("{0} exactly equals {1}: {2}",
                  otherTime, universalTime,
                  universalTime.EqualsExact(otherTime)) + "\n";
outputBlock.Text += "\n";
// The example produces the following output:
//    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   

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.