Check if DateTime is based on local time, Coordinated Universal Time (UTC), or neither in CSharp

Description

The following code shows how to check if DateTime is based on local time, Coordinated Universal Time (UTC), or neither.

Example


//www. j ava 2s .  co  m
using System;

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 »
    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