3

I'm trying to display only date part on my view, value I'm getting from database is pretty big and I really don't need informations like hrs, mns, seconds etc, I'm only interested in date, for example I would like to display only :

17/04/2017 

And this is what I've tried so far:

<td>
       @Html.DisplayFor(modelItem => ((DateTime)item.ValidFrom).ToString("dd/MM/yyyy"))
</td>
<td>
       @Html.DisplayFor(modelItem => ((DateTime)item.ValidTo).ToString("dd/MM/yyyy"))
</td>

Here is the DateType of my Properties:

public System.DateTime ValidFrom { get; set; }
public Nullable<System.DateTime> ValidTo { get; set;

And here is the issue I get: [![enter image description here][1]][1]

3 Answers 3

3

Just Output Directly

Instead of using the DisplayFor() helper method, you might consider just outputting the values directly if that would work for you:

<td>
   @modelItem.ValidFrom.ToString("dd/MM/yyyy")
</td>
<td>
   @modelItem.ValidTo?.ToString("dd/MM/yyyy")
</td>

Consider ViewModel-Level Formatting

Another possibly better option, would be performing this within your ViewModel itself as opposed to within the View:

public System.DateTime ValidFrom { get; set; }
public Nullable<System.DateTime> ValidTo { get; set; }

// These will be formatted versions of the properties specifically for output
public string FormattedFrom => ValidFrom.ToString("dd/MM/yyyy");
public string FormattedTo => ValidTo?.ToString("dd/MM/yyyy") ?? "Present";

And then simply use those properties instead:

<td>
   @Model.FormattedFrom
</td>

<td>
   @Model.FormattedTo
</td>
Sign up to request clarification or add additional context in comments.

10 Comments

This is cool I will accept answer soon, but what's gonna happen when I upadate my model from database than stuffs I added in my class will be lost, how could I solve this, somehow with partial classes or whatever?
and when I apply this code I am getting issue : "ValidTo" is property but is used like a type?
Those properties FormattedFrom and FormattedTo are essentially only getters, and thus will only use whatever data is present in the model itself, so you should be safe moving forward with that code. Additionally, there was a syntax error within the previous FormattedTo declaration, which should be resolved now.
outputting the values directly might work for me, in first case ValidFrom is displayed clear, but in second case when I add " ? " than it display this : 31.12.3000 0:00:00?.ToString("dd/MM/yyyy")) ....
It may not like the newer C# syntax, try this @(modelItem.ValidTo.HasValue ? modelItem.ValidTo.ToString("dd/MM/yyyy") : "Present").
|
2

The solution might not be great in looking but I tried it and working for me. You can try and use it for faster and temporary time.

@Html.FormatValue("yyy-MM-dd", item.date.ToString("yyy-MM-dd"))

Comments

0

This works for me:

<dd>
      @Convert.ToDateTime(Model.insertDateTimeFiled).ToString("dd-MM-yyyy")
</dd>

If you only write:

Model.insertDateTimeFiled.ToString("dd-MM-yyyy")

it trows "No overload for method 'ToString()' takes 1 argument

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.