0

i m getting date from sql server database and it is displaying datetime as 1/15/2015 12:00:00 AM.

code in c# i used is:

dob_lbl.Text = reader[6].ToString();

//this is extracted using Sqlconnection so it's in array format.

i need only date to display.pls help.

alex dave
  • 93
  • 1
  • 3
  • 10

3 Answers3

4

try this:

string strDate = reader[6].ToString();
dob_lbl.Text  =  DateTime.ParseExact(strDate, "M/dd/yyyy hh:mm:ss tt", 
                            CultureInfo.InvariantCulture).ToString("yyyy/MM/dd");
Sudhakar Tillapudi
  • 25,307
  • 5
  • 35
  • 65
1

The ToShortDateString method of DateTime should help with this.

Use it like:

string strDate = reader[6].ToString();
DateTime dateTime = DateTime.Parse(strDate);
string justDateStr = dateTime.ToShortDateString();
Derek W
  • 9,368
  • 5
  • 53
  • 66
0

You could simply split the string and use the first part of the resulting array. Something like:

dob_lbl.Text = reader[6].ToString().Split()[0];
bit
  • 4,271
  • 1
  • 27
  • 47