Jack.D
Jack.D

Reputation: 49

Can't convert date value get from SQL database

I had set the date datatype to date in sql server but when i want get value from it will output datetime value "9/22/2016 12:00:00 AM".

DataGridViewRow row = cell.OwningRow;
string DATE = row.Cells[2].Value.ToString();

enter image description here

Upvotes: 0

Views: 135

Answers (4)

Raviteja
Raviteja

Reputation: 257

Convert string into datetime and you can give the format like this way ... it will works....

 DateTime d=Convert.ToDateTime(row.Cells[2].Value.ToString());
 textBox1.Text = d.ToShortDateString();

Upvotes: 0

Mukul Varshney
Mukul Varshney

Reputation: 3141

Convert the date to the desired format

DateTime DATE = Convert.ToDateTime(row.Cells[2].Value.ToString();

When you want to display the DATE, use the format "yyyy-MM-dd" (as per your screen shot).

Upvotes: 0

Vadim Martynov
Vadim Martynov

Reputation: 8892

Ok, you are usting ToString() method and it returns string value. You can retrieve DateTime with explicit casting:

DataGridViewRow row = cell.OwningRow;
DateTime DATE = (DateTime) row.Cells[2].Value;

Upvotes: 2

connectedsoftware
connectedsoftware

Reputation: 7087

You need to format it on the front end:

((DateTime)row.Cells[2].Value).ToString("dd-MM-yyyy");

Upvotes: 3

Related Questions