How to Get Only Date from a Datetime Value in Razor Page

How to get only date from a DateTime value in Razor page?

If your using TextBoxFor you can use the following to show only the date -

@Html.TextBoxFor(model => model.Date, "{0:MM/dd/yyyy}")

Display only date and no time

If the column type is DateTime in SQL then it will store a time where you pass one or not.

It'd be better to save the date properly:

model.ReturnDate = DateTime.Now;

and then format it when you need to display it:

@Html.Label(Model.ReturnDate.ToShortDateString())

Or if you're using EditorFor:

@Html.EditorFor(model => model.ReturnDate.ToShortDateString())

or

@Html.EditorFor(model => model.ReturnDate.ToString("MM/dd/yyyy"))

To add a property to your model add this code:

public string ReturnDateForDisplay
{
get
{
return this.ReturnDate.ToString("d");
}
}

Then in your PartialView:

@Html.EditorFor(model => model.ReturnDateForDisplay)

EDIT:

I just want to clarify for this answer that by my saying 'If you're using EditorFor', that means you need to have an EditorFor template for the type of value you're trying to represent.

Editor templates are a cool way of managing repetitive controls in MVC:

http://coding-in.net/asp-net-mvc-3-how-to-use-editortemplates/

You can use them for naive types like String as I've done above; but they're especially great for letting you template a set of input fields for a more complicated data type.

How to display only DATE part on View in ASP.NET MVC

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>

ASP.Net MVC Razor Remove Time From DateTime Property

You can use @item.startDate.Value.ToShortDateString() (adding the proper validation for null value)

MVC Razor view displays the wrong time value of a DateTime object

That's because of your display template: you are passing only date part to the string.Format function.
Try to just pass a model(your date time object) like this:

@String.Format("{0:dd-MM-yyyy hh:mm tt}", Model)

Also IMHO it's cleaner to just call .ToString(string format) method on the date object:

@Model.ToString("dd-MM-yyyy hh:mm tt")

Another way to format dates is to create a view-model class and use[DisplayFormat] attribute to specify date time format(it should work for EditorFor as well).



Related Topics



Leave a reply



Submit