DateTimeOffset.Implicit(DateTime to DateTimeOffset) Operatore
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Definisce una conversione implicita di un DateTime oggetto in un DateTimeOffset oggetto .
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
Parametri
- dateTime
- DateTime
Oggetto da convertire.
Restituisce
Oggetto convertito.
Eccezioni
La data e l'ora UTC (Coordinated Universal Time) risultanti dall'applicazione dell'offset sono precedenti a DateTimeOffset.MinValue.
oppure
La data e l'ora UTC risultanti dall'applicazione dell'offset sono successive a DateTimeOffset.MaxValue.
Commenti
Il Implicit metodo consente al compilatore di convertire automaticamente un DateTime oggetto in un DateTimeOffset oggetto senza un operatore di cast esplicito (in C#) o una chiamata a una funzione di conversione (in Visual Basic). Definisce una conversione più ampia che non comporta la perdita di dati e non genera un'eccezione OverflowException. Il Implicit metodo rende possibile il codice, ad esempio quanto segue:
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.
let timeWithOffset = DateTime(2008, 7, 3, 18, 45, 0)
printfn $"{timeWithOffset}"
let timeWithOffset = DateTime.UtcNow
printfn $"{timeWithOffset}"
let timeWithOffset =
DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Unspecified)
printfn $"{timeWithOffset}"
let timeWithOffset =
DateTime(2008, 7, 1, 2, 30, 0)
+ TimeSpan(1, 0, 0, 0)
printfn $"{timeWithOffset}"
let timeWithOffset = DateTime(2008, 1, 1, 2, 30, 0)
printfn $"{timeWithOffset}"
// 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.
Questo metodo equivale al DateTimeOffset costruttore. L'offset dell'oggetto risultante DateTimeOffset dipende dal valore della DateTime.Kind proprietà del dateTime parametro :
Se il valore della DateTime.Kind proprietà è DateTimeKind.Utc, la data e l'ora dell'oggetto DateTimeOffset sono impostate su
dateTimee la relativa Offset proprietà è impostata su 0.Se il valore della DateTime.Kind proprietà è DateTimeKind.Local o DateTimeKind.Unspecified, la data e l'ora dell'oggetto DateTimeOffset sono impostate su
dateTimee la relativa Offset proprietà è impostata su uguale all'offset del fuso orario corrente del sistema locale.
Il metodo equivalente per questo operatore è DateTimeOffset.DateTimeOffset(DateTime)