Reputation: 137
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
Reputation: 153
Below Code can Help You
strInvoice_date = Convert.ToDateTime(dt.Rows[i]["INVOICE_DATE"].ToString("0:dd/MM/yyyy"));
Upvotes: 0
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
Reputation: 186843
Convert and then represent as you want:
strInvoice_date = Convert.ToDateTime(dt.Rows[i]["INVOICE_DATE"]).ToString("dd/MM/yyyy");
Upvotes: 6