0

How can I remove the Time portion from DateTime before passing it to a GridView. I'm using VS2008 else I would be using Date :(

  DataTable datatablePlanning = new DataTable("Planning");

  DateTime begindate = DatePicker1.SelectedDate;

  DataRow detailregel = datatablePlanning.NewRow();
  detailregel["Date"] = begindate;
  datatablePlanning.Rows.Add(detailregel);

  GridView2.DataSource = datatablePlanning;
  GridView2.DataBind();
1
  • 1
    You can't remove it. You can set it to zeroes. What you probably want to do is set the format of the DateTime for that column. Commented Oct 19, 2012 at 10:07

5 Answers 5

3

You can format your date.

detailregel["Date"] = begindate.ToString("dd/MM/yyyy");

or you can try

detailregel["Date"] = begindate.ToShortDateString();

For Different Format you can look at

Custom Date and Time Format Strings

Sign up to request clarification or add additional context in comments.

2 Comments

mm => MM, ToShort => ToShortDateString?
yeah already changed the mm to MM, thanks for the heads up though:)
2

You can format Date as follow...

detailregel["Date"] = begindate.ToString("dd/MM/yyyy"); 

You can find fore info DateTime Properties

Comments

1

you can give the text property of binding text box like this Text='<%# Bind("Date","{0:MM/dd/yyyy}") %>'

Comments

1

You can also format in design mode using EVAL in grid view item template:

   <%# Convert.ToDate(Eval("Your_Date")).ToShortDateString()%>

Comments

1

If you are not bothered about the time part of the date in the gridview, just format it to show the date part

<asp:Label ID="lblDate" runat="server" Text='<%# Eval("Date", "{0:MM/d/yyyy}")%>'></asp:Label>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.