DateTimeOffset.Implicit(DateTime to DateTimeOffset) Оператор
Определение
Важно!
Некоторые сведения относятся к предварительной версии продукта, в которую до выпуска могут быть внесены существенные изменения. Майкрософт не предоставляет никаких гарантий, явных или подразумеваемых, относительно приведенных здесь сведений.
Определяет неявное преобразование DateTime объекта в DateTimeOffset объект.
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
Параметры
- dateTime
- DateTime
Объект для преобразования.
Возвращаемое значение
Преобразованный объект.
Исключения
Дата и время в формате UTC, полученные от применения смещения, ранее , чем DateTimeOffset.MinValue.
–или–
Дата и время UTC, результаты применения смещения позже , чем DateTimeOffset.MaxValue.
Комментарии
Метод Implicit позволяет компилятору автоматически преобразовывать DateTime объект в объект DateTimeOffset без явного оператора приведения (в C#) или вызова функции преобразования (в Visual Basic). Он определяет расширение преобразования, которое не включает потерю данных и не создает OverflowExceptionисключение. Этот Implicit метод делает код возможным следующим образом:
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.
Этот метод эквивалентен конструктору DateTimeOffset . Смещение результирующего DateTimeOffset объекта зависит от значения DateTime.Kind свойства dateTime параметра:
Если значение DateTime.Kind свойства равно DateTimeKind.Utc, то дата и время DateTimeOffset объекта равны
dateTime, а его Offset свойство равно 0.Если значение DateTime.Kind свойства равно DateTimeKind.Local или DateTimeKind.Unspecified, дата и время DateTimeOffset объекта равно
dateTime, а его Offset свойство равно смещение текущего часового пояса локальной системы.
Эквивалентный метод для этого оператора DateTimeOffset.DateTimeOffset(DateTime)