Azri Zakaria
Azri Zakaria

Reputation: 1364

Date format in LINQ

i got problem with my LINQ to format date. Here is code

 var resultCustomer = from row in formDg.dtCustomer.AsEnumerable()
                          where row.Field<int>("customerID") == customerID2
                          select new
                          {
                              name = row["customerName"].ToString(),
                              ic = row["customerIC"].ToString(),
                              add1 = row["customerAdd1"].ToString(),
                              add2 = row["customerAdd2"].ToString(),
                              tel = row["customerTel"].ToString(),
                              tel2 = row["customerTel2"].ToString(),
                              tel3 = row["customerTel3"].ToString(),
                              email = row["customerEmail"].ToString(),
                              dateRegister = row["customerDateRegister"].ToString(),
                              customerRef = row["customerRef"].ToString(),
                              customerGroup = row["groupCustName"].ToString(),
                              panelName = row["panelName"].ToString(),
                              };

var firstRecord = resultCustomer.First();// My record return single row only

My question is how i can format custom date like "dd/MM/yyyy" in customerDateRegister?, Because I got problem when my textbox show 16-Nov-12 12:00:00 AM. I don`t want to display time but just date only.

I have try like this tbDateRegOv.Text = firstRecord.dateRegister.ToString("dd/MM/yyyy");. not work. Then i try dateRegister = row["customerDateRegister"].ToString("dd/MM/yyyy"), in LINQ. Same problem.

So how can I solved this problem? Thanks for help. :)

Upvotes: 1

Views: 7302

Answers (4)

checho
checho

Reputation: 3120

Or, you can use MaskedEditBox with the desired datetime mask

Upvotes: 0

Dave Zych
Dave Zych

Reputation: 21887

You need to cast your row as a DateTime first. This will allow you to use the custom format strings for dates.

dateRegister = ((DateTime)row["customerDateRegister"]).ToString("dd/MM/yyyy")

Upvotes: 3

Ravi Y
Ravi Y

Reputation: 4376

You could try this , replace your dateRegister line with this.

dateRegister = ((DateTime)row["customerDateRegister"]).ToString("dd/MM/yyyy"),

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460108

If customerDateRegister is of type DateTime you should make it a DateTime in the first place. Therefor use the DataRow extension method Field<T>:

dateRegister = row.Field<DateTime>("customerDateRegister")

Upvotes: 2

Related Questions