DateTimeOffset.Implicit(DateTime to DateTimeOffset) 运算符

定义

定义从 DateTime 对象到 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

参数

dateTime
DateTime

要转换的对象。The object to convert.

返回

转换后的对象。The converted object.

异常

通过应用偏移量得出的协调世界时 (UTC) 日期和时间早于 MinValueThe Coordinated Universal Time (UTC) date and time that results from applying the offset is earlier than MinValue.

-或--or- 通过应用偏移量得出的 UTC 日期和时间晚于 MaxValueThe UTC date and time that results from applying the offset is later than MaxValue.

注解

使用 Implicit 方法,编译器可以自动将 DateTime 对象转换为 DateTimeOffset 对象,而无需使用显式强制转换运算符C#(在中)或调用转换函数(在 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). 它定义了不涉及数据丢失的扩大转换,并且不会引发 OverflowExceptionIt defines a widening conversion that does not involve data loss and does not throw an OverflowException. Implicit 方法会使代码如下所示: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.

此方法等效于 DateTimeOffset 构造函数。This method is equivalent to the DateTimeOffset constructor. 生成的 DateTimeOffset 对象的偏移量取决于 dateTime 参数的 DateTime.Kind 属性值:The offset of the resulting DateTimeOffset object depends on the value of the DateTime.Kind property of the dateTime parameter:

此运算符的等效方法是 DateTimeOffset.DateTimeOffset(DateTime)The equivalent method for this operator is DateTimeOffset.DateTimeOffset(DateTime)

适用于