C# DateTime Kind

Description

DateTime Kind gets a value that indicates whether the time represented by this instance is based on local time, Coordinated Universal Time (UTC), or neither.

Syntax

DateTime.Kind has the following syntax.


public DateTimeKind Kind { get; }

Example

The following example uses the SpecifyKind method to demonstrate how the Kind property influences the ToLocalTime and ToUniversalTime conversion methods.


using System;//from   www  . j  ava  2  s .c  om

class Sample 
{
    public static void Main() 
    {
        DateTime saveNow = DateTime.Now;
    
        DateTime saveUtcNow = DateTime.UtcNow;
        DateTime myDt;
    
        myDt = DateTime.SpecifyKind(saveNow, DateTimeKind.Utc);
        Display("Utc: .............", myDt);

    }

    public static string datePatt = @"M/d/yyyy hh:mm:ss tt";
    public static void Display(string title, DateTime inputDt)
    {
        DateTime dispDt = inputDt;
        string dtString;
    
        dtString = dispDt.ToString(datePatt);
        Console.WriteLine("{0} {1}, Kind = {2}", 
                          title, dtString, dispDt.Kind);
    }

}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version