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
  • 115
  • 1
  • 4
  • 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,935
  • 5
  • 37
  • 67
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,708
  • 5
  • 58
  • 67
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,407
  • 1
  • 28
  • 50