-2

I need to get date from datetime in linq like this:

var items = db.Students.ToList().Select(u => new { bi = u.Birthday.Date });

but items in grid are displayed as follows:

enter image description here

I've seen other questions:

Question1: Linq - Select Date from DateTime

but when I edit code Similarly,

                var items = (from xx in db.Students                                     
                                 select new
                                 {
                                    EntityFunctions.TruncateTime(xx.Birthday)
                                 }).ToList();

I got a error in code!!

Question2: How to get only Date from datetime column using linq

but accepted answer to this question convert date to string!

Question3: Linq to Entity get a Date from DateTime

Answers similar to question1.

aspx for show grid:

<asp:GridView ID="GridView1" runat="server">            
</asp:GridView>

Does anyone have an idea to solve this problem?

Thanks.

Community
  • 1
  • 1
Ali Soltani
  • 9,589
  • 5
  • 30
  • 55
  • where is the DateTime coming from? Is it a string or a DateTime object? – jdweng Jul 07 '16 at 09:08
  • Looks like while fetching the date you are getting only the date part, but while displaying you are not formatting. Before displaying your grid try to format using .ToString("dd/MM/yyyy") – Abhilash R Vankayala Jul 07 '16 at 09:08
  • Change the grid column's format attribute to whatever you want. What grid are you using? Where is the ASP.NET code that generates the grid? – Panagiotis Kanavos Jul 07 '16 at 09:09
  • You should really start using the search option. There are a million questions how to format a date. This question is no different. – Patrick Hofman Jul 07 '16 at 09:10
  • @PanagiotisKanavos this grid is simple for simplicity. I add aspx. – Ali Soltani Jul 07 '16 at 09:14
  • The [real duplicate](http://stackoverflow.com/questions/22449788/date-format-without-time-in-asp-net-gridview) shows how toformat data while data binding – Panagiotis Kanavos Jul 07 '16 at 09:16
  • @alisoltani *which* control are you using? Different controls have different properties, although I suspect you should really check a tutorial on ASP.NET data binding. Check the link in the previous comment – Panagiotis Kanavos Jul 07 '16 at 09:16
  • @PanagiotisKanavos You can't know the 'real' duplicate since OP doesn't tell how he binds the data. Does he even bind the data? The previous question used `string.Format` so I have chosen to stay with that and close as this duplicate. I would welcome any other duplicate target though. – Patrick Hofman Jul 07 '16 at 09:18
  • @PanagiotisKanavos Ah, sorry. Just got the update question from OP indeed showing a regular ASPX gridview :) – Patrick Hofman Jul 07 '16 at 09:18
  • @PatrickHofman duplicate problem??!! – Ali Soltani Jul 07 '16 at 09:30
  • @PatrickHofman this is not exactly duplicate i voted for reopen – fubo Jul 07 '16 at 09:36
  • It was at the time of writing. Feel free to reopen it and close as the duplicate proposed by Panagiotis @fubo – Patrick Hofman Jul 07 '16 at 09:38

1 Answers1

0

As you have been told before in your other questions; when you display your grid at some point DateTime.ToString() is called, this results in a default behavior of displaying date and time. The DateTime.Date property simply returns the date part of the DateTime as a DateTime; in other words it returns the same thing but with the time set to exactly 12:00 AM.

To get the only the date as a string, you want to call DateTime.ToShortDateString, see here: https://msdn.microsoft.com/en-us/library/system.datetime.toshortdatestring(v=vs.110).aspx

In short this has nothing to do with Linq, the problem occurs when you try to display your DateTime object within your grid and as such needs to be solved by adjusting the formatting of the grid cells.

Alexei Barnes
  • 268
  • 1
  • 9
  • No need for that. Most grids have format attributes for their columns. Even if they don't formatting in the ASPX file or view file is the best place to format the value. – Panagiotis Kanavos Jul 07 '16 at 09:11
  • @PanagiotisKanavos Yes, I'll reword my statement as that was what I was intending to say, but obviously I wasn't clear enough. – Alexei Barnes Jul 07 '16 at 09:12
  • The OP was calling `String.Format` right inside the LINQ statement in the previous question. It isn't possible to answer correctly this question in its current form – Panagiotis Kanavos Jul 07 '16 at 09:14
  • @PanagiotisKanavos I think the OP is confused about the cause of the problem, and so I'm attempting to resolve that confusion. I'll admit that I'm answering the implied question rather than the literal question. – Alexei Barnes Jul 07 '16 at 09:15