DateTimeOffset.Implicit(DateTime to DateTimeOffset) Operador

Definición

Define una conversión implícita de un objeto DateTime en un objeto DateTimeOffset.Defines an implicit conversion of a DateTime object to a DateTimeOffset object.

public:
 static operator DateTimeOffset(DateTime dateTime);
public static implicit operator DateTimeOffset (DateTime dateTime);
static member op_Implicit : DateTime -> DateTimeOffset
Public Shared Widening Operator CType (dateTime As DateTime) As DateTimeOffset

Parámetros

dateTime
DateTime

Objeto que se va a convertir.The object to convert.

Devoluciones

DateTimeOffset

El objeto convertido.The converted object.

Excepciones

La fecha y hora UTC (hora universal coordinada) que resulta de aplicar el desplazamiento es anterior a MinValue.The Coordinated Universal Time (UTC) date and time that results from applying the offset is earlier than MinValue.

o bien-or- La fecha y hora UTC que resulta de aplicar el desplazamiento es posterior a MaxValue.The UTC date and time that results from applying the offset is later than MaxValue.

Comentarios

El Implicit método permite que el compilador convierta automáticamente un DateTime objeto en un DateTimeOffset objeto sin un operador de conversión explícito (en C#) o una llamada a una función de conversión (en Visual Basic).The Implicit method enables the compiler to automatically convert a DateTime object to a DateTimeOffset object without an explicit casting operator (in C#) or a call to a conversion function (in Visual Basic). Define una conversión de ampliación que no implica la pérdida de datos y no produce una excepción OverflowException .It defines a widening conversion that does not involve data loss and does not throw an OverflowException. El Implicit método hace posible el código como el siguiente:The Implicit method makes code such as the following possible:

DateTimeOffset timeWithOffset;
timeWithOffset = new DateTime(2008, 7, 3, 18, 45, 0);
Console.WriteLine(timeWithOffset.ToString());

timeWithOffset = DateTime.UtcNow;
Console.WriteLine(timeWithOffset.ToString());

timeWithOffset = DateTime.SpecifyKind(DateTime.Now,
                                      DateTimeKind.Unspecified);
Console.WriteLine(timeWithOffset.ToString());

timeWithOffset = new DateTime(2008, 7, 1, 2, 30, 0) +
                       new TimeSpan(1, 0, 0, 0);
Console.WriteLine(timeWithOffset.ToString());

timeWithOffset = new DateTime(2008, 1, 1, 2, 30, 0);
Console.WriteLine(timeWithOffset.ToString());
// The example produces the following output if run on 3/20/2007
// at 6:25 PM on a computer in the U.S. Pacific Daylight Time zone:
//       7/3/2008 6:45:00 PM -07:00
//       3/21/2007 1:25:52 AM +00:00
//       3/20/2007 6:25:52 PM -07:00
//       7/2/2008 2:30:00 AM -07:00
//       1/1/2008 2:30:00 AM -08:00
//
// The last example shows automatic adaption to the U.S. Pacific Time
// for winter dates.
   Dim timeWithOffset As DateTimeOffset 
   timeWithOffset = #07/03/2008 6:45PM#
   Console.WriteLine(timeWithOffset.ToString())
   
   timeWithOffset = Date.UtcNow
   Console.WriteLine(timeWithOffset.ToString())
   
   timeWithOffset = Date.SpecifyKind(Date.Now, DateTimeKind.Unspecified)
   Console.WriteLine(timeWithOffset.ToString())

   timeWithOffset = #07/01/2008 2:30AM# + New TimeSpan(1, 0, 0, 0)
   Console.WriteLine(timeWithOffset.ToString())
     
   timeWithOffset = #01/01/2008 2:30AM#
   Console.WriteLine(timeWithOffset.ToString())
   ' The example produces the following output if run on 3/20/2007 
   ' at 6:25 PM on a computer in the U.S. Pacific Daylight Time zone:
   '       7/3/2008 6:45:00 PM -07:00
   '       3/21/2007 1:25:52 AM +00:00
   '       3/20/2007 6:25:52 PM -07:00
   '       7/2/2008 2:30:00 AM -07:00
   '       1/1/2008 2:30:00 AM -08:00
   '
   ' The last example shows automatic adaption to the U.S. Pacific Time
   ' for winter dates.

Este método es equivalente al DateTimeOffset constructor.This method is equivalent to the DateTimeOffset constructor. El desplazamiento del objeto resultante DateTimeOffset depende del valor de la DateTime.Kind propiedad del dateTime parámetro:The offset of the resulting DateTimeOffset object depends on the value of the DateTime.Kind property of the dateTime parameter:

El método equivalente para este operador es DateTimeOffset.DateTimeOffset(DateTime)The equivalent method for this operator is DateTimeOffset.DateTimeOffset(DateTime)

Se aplica a