2

Hi I am trying to get the date from a sql table however whenever I get it, it returns with the time. Even though in the database table it does not have a time.

        con.Open();
        OdbcCommand cmd = con.CreateCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "Select I_InvoiceNo, I_Date from tblOrdInvoice where O_OrderNo = '" + orderNoTxt.Text + "'";
        cmd.ExecuteNonQuery();
        con.Close();
        DataTable dt = new DataTable();
        OdbcDataAdapter sda = new OdbcDataAdapter(cmd);
        sda.Fill(dt);
        foreach (DataRow dr in dt.Rows)
        {
            invoiceNoTxt.Text = dr["I_InvoiceNo"].ToString();
            invoiceDateTxt.Text = dr["I_Date"].ToString();
        }

The date is being returned as "27/03/2017 00:00:00" . I only want the date. Any Help please.

zayn94
  • 65
  • 1
  • 11

2 Answers2

1

Change the code to:

string dateFormat = "dd-MMM-yyyy";
invoiceDateTxt.Text = dr.Field<DateTime>("I_Date").ToString(dateFormat);

Change your dateFormat variable to whatever format you like.

Sadique
  • 22,572
  • 7
  • 65
  • 91
0

You can convert DateTime to whichever format you want from code side. For example by using :

invoiceDateTxt.Text = dr["I_Date"].ToString("dd/MM/yyyy");

Or if you want to fetch only date part from mysql , then you can use something like below:

SELECT CONVERT(VARCHAR(10), GETDATE(), 112) 'Date Part Only'

RESULT:
Date Part Only
--------------
20170327
Example 2:

SELECT CONVERT(VARCHAR(10), GETDATE(), 111) 'Date Part Only'

RESULT:
Date Part Only
--------------
2017/03/27

For more details visit this link.

Vandita
  • 708
  • 4
  • 13