0

I have a Property

[DataType(DataType.Date)]
public DateTime? DatePost { get; set; }

And I want to format the DateTime for each item of this property.

Select(x => new
                    {
                        x.Price,
                        x.ItemsCount,
                        x.DatePost});
var result = query.toList();

So I was trying to use a foreach to get each property and change to ShortDateString, but the result of query it always come with the time together. The format has to be like this "dd/MM/yyyy".

List<dynamic> l = new List<dynamic>();
                var result = users.ToList();
                foreach (var item in result)
                {
                    item.DatePost?.ToShortDateString();
                    l.Add(item);
                }
return Ok(l);

The result is :

2017-04-15T23:53:00.193
  • http://stackoverflow.com/questions/13351109/datatypedatatype-date-format-mvc – Tim Apr 16 '17 at 22:40
  • I did change, and it doesn't work! –  Apr 16 '17 at 22:43
  • The line before the l.Add(item) is not changing the datepost value. You are adding a user objects (w/ the whole DateTime value) still.. You should be able to decorate your entity model with annotation for this property to treat this field as a date. [Column(TypeName="date")], so it persists only date portion. https://msdn.microsoft.com/en-in/data/jj591583 – Tim Apr 16 '17 at 22:57
  • I did change too, the result is the same, but now the Time came with zeros. 2017-04-15T00:00:00. –  Apr 16 '17 at 23:07
  • so, the problem is just how the field renders? in your Select() statement, could you just use x.DatePost.ToShortDateString() so you only represent this as a string? – Tim Apr 16 '17 at 23:19
  • I tried, but i got this "Anonymous type members must be declared with a member assignment,simple member or access", so I did this "verbose" : ´DatePost = x.DatePost.HasValue ? x.DatePost.Value.ToShortDateString() : "Unknown"´ I and got a exception : "ToShortDateString(), this method cannot be translated into a store expression", I just want to retrieve the Date without Time! –  Apr 16 '17 at 23:49
  • DateTime formatting is a presentation item, and formatting is done when you are displaying it. Have you tried doing it the old fashioned low overhead way of just doing it on the final View itself? – Mad Myche Apr 17 '17 at 00:44
  • I'am consuming this Api on Android,so i don't know any of other ways to do this, normally DateTime.ToShortDateString() works fine, but the problem apparently is with the Entity Framework when it was cast to DateTime structure. [here](http://stackoverflow.com/questions/43020809/how-to-format-a-date-into-sql-server-using-entityframework/43021149#comment73129732_43020956) –  Apr 17 '17 at 00:59
  • see if **SqlFunctions.DatePart** suits your need - https://msdn.microsoft.com/en-us/library/system.data.entity.sqlserver.sqlfunctions(v=vs.113).aspx You could get separately the Day, Month and Year parts and concatenate them inside the lambda expression itself (it's just that it won't be DateTime type anymore).... – gkb Apr 17 '17 at 08:12

0 Answers0