-5

Possible Duplicate:
extract the date part from DateTime in C#

I have this code that display the date.

DateTime dt = DateTime.ParseExact(date1,"ddMMyy",System.Globalization.CultureInfo.CurrentCulture);

And it gives me an output of 6/12/2012 12:00:00 AM.

But what I need to display is the date only, how can I remove the time so that the only one to be display is the date?

Community
  • 1
  • 1
juanDiola
  • 3
  • 2
  • 3

3 Answers3

4

Use the method ToShortDateString() of DateTime

dt.ToShortDateString();

See here for refs

The string returned by the ToShortDateString method is culture-sensitive. It reflects the pattern defined by the current culture's DateTimeFormatInfo object.

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
Steve
  • 213,761
  • 22
  • 232
  • 286
2

This one is what you need

String.Format("{0:M/d/yyyy}", dt);    // "3/9/2008"

See more here :string-format-datetime

PresleyDias
  • 3,657
  • 6
  • 36
  • 62
levi
  • 3,451
  • 6
  • 50
  • 86
0

The patterns for DateTime.ToString ('d') :

 DateTime dt = DateTime.ParseExact(date1,"ddMMyy",System.Globalization.CultureInfo.CurrentCulture);

// Get date-only portion of date, without its time.
DateTime dateOnly = dt.Date;
// Display date using short date string.
Console.WriteLine(dateOnly.ToString("d"));

Also check out : DateTime.ToString() Patterns

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263