Do an implicit conversion of a DateTime object to a DateTimeOffset object in CSharp

Description

The following code shows how to do an implicit conversion of a DateTime object to a DateTimeOffset object.

Example


/*  w ww .ja  v  a 2 s.co m*/
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.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var