C# DateTime Date

Description

DateTime Date gets the date component of this instance.

Syntax

DateTime.Date has the following syntax.


public DateTime Date { get; }

Example

The following example shows how to get the date component of this instance.

It uses the Date property to extract the date component of a DateTime value with its time component set to zero (or 0:00:00, or midnight).

It also illustrates that, depending on the format string used when displaying the DateTime value, the time component can continue to appear in formatted output.


using System;//from  w  ww.  j  a  v a 2  s. c o  m
public class MainClass{
  public static void Main(String[] argv){  
    DateTime date1 = new DateTime(2008, 6, 1, 7, 47, 0);
    Console.WriteLine(date1.ToString());

    DateTime dateOnly = date1.Date;

    // Display date using short date string.
    System.Console.WriteLine(dateOnly.ToString("d"));

    // Display date using 24-hour clock.
    System.Console.WriteLine(dateOnly.ToString("g"));
    System.Console.WriteLine(dateOnly.ToString("MM/dd/yyyy HH:mm"));   
  }
}
    

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