C# DateTimeOffset Implicit(DateTime to DateTimeOffset)
Description
DateTimeOffset Implicit(DateTime to DateTimeOffset)
defines
an implicit conversion of a DateTime object to a DateTimeOffset object.
Syntax
DateTimeOffset.Implicit(DateTime to DateTimeOffset)
has the following syntax.
public static implicit operator DateTimeOffset (
DateTime dateTime
)
Parameters
DateTimeOffset.Implicit(DateTime to DateTimeOffset)
has the following parameters.
dateTime
- The object to convert.
Returns
DateTimeOffset.Implicit(DateTime to DateTimeOffset)
method returns The converted object.
Example
The Implicit method enables the compiler to automatically convert a DateTime object to a DateTimeOffset object without an explicit casting operator in C#.
/*from w w w . jav a 2 s .c om*/
using System;
public class MainClass{
public static void Main(String[] argv){
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(2014, 1, 1, 2, 30, 0) +
new TimeSpan(1, 0, 0, 0);
Console.WriteLine(timeWithOffset.ToString());
}
}
The code above generates the following result.