ash060
ash060

Reputation: 137

Convert datatable date into dd/mm/yyyy format in asp.net

I have a datatable in which I want to get the date in dd/mm/yyyy format.

Currently I get date like

02-01-2012 12:00:00 AM

Below is my code

strInvoice_date = dt.Rows[i]["INVOICE_DATE"].ToString();

Upvotes: 3

Views: 12916

Answers (4)

JENKINS J
JENKINS J

Reputation: 153

Below Code can Help You

strInvoice_date = Convert.ToDateTime(dt.Rows[i]["INVOICE_DATE"].ToString("0:dd/MM/yyyy"));

Upvotes: 0

Soner Gönül
Soner Gönül

Reputation: 98868

Your dt.Rows[i]["INVOICE_DATE"] retuns object and when you call ToString method, it will call object.ToString() not DateTime.ToString().

If your INVOICE_DATE column is DateTime in your datatable, you can explicitly cast your object to DateTime and use dd/MM/yyyy (I assume you want months instead of minutes) format with a proper culture like InvariantCulture.

var Invoice_date = (DateTime)dt.Rows[i]["INVOICE_DATE"]
                               .ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);

If your INVOICE_DATE column is string in your datatable, you need to get it's string representation with object.ToString, parse it to Datetime, and generate it's string representation.

var Invoice_date = DateTime.ParseExact(dt.Rows[i]["INVOICE_DATE"].ToString(), 
                                      "dd-MM-yyyy hh:mm:ss tt", 
                                       CultureInfo.InvariantCulture)
                          .ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);

Upvotes: 3

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186843

Convert and then represent as you want:

  strInvoice_date = Convert.ToDateTime(dt.Rows[i]["INVOICE_DATE"]).ToString("dd/MM/yyyy");

Upvotes: 6

M.S.
M.S.

Reputation: 4443

You've to pass the required date format in ToString() method,

 strInvoice_date = Convert.ToDateTime(dt.Rows[i]["INVOICE_DATE"]).ToString("dd/mm/yyyy");

For more details on date time formatting, visit

Upvotes: 0

Related Questions