Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1021191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:16:25+00:00 2026-05-16T11:16:25+00:00

Code: <% foreach (var item in Model) { %> <td> <%= Html.Encode(item.BirthDate) %> </td>

  • 0

Code:

  <% foreach (var item in Model) { %>   
  <td>
     <%= Html.Encode(item.BirthDate) %>
  </td>
  <% } %>

display this: 8/24/2009 12:00:00 AM but I need only date (8/24/2009). It is possible to do without any formating in controller action

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T11:16:25+00:00Added an answer on May 16, 2026 at 11:16 am

    There are a couple of ways to go about it. If you’re using MVC 2, you could use a DisplayTemplate. Just put a DateTime.ascx file in the folder called /Views/Shared/DisplayTemplates and the line of code it would have in it is:

    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.DateTime?>" %>
    <%: Model.HasValue ? Model.Value.ToShortDateString() : string.Empty %>
    

    (Note: if you use the <%: syntax then you don’t need the Html.Encode() because <%: does the HTML encoding for you – but that’s only if you’re using VS2010. If you’re using MVC 2 with VS2008, then stick with the Html.Encode() for this part) Then in your view you would simply do this:

    <%: Html.DisplayFor(m => m.BirthDate) %>
    

    That will change the format to only have the Date for all DateTime’s in your application. Of course you could put that directly in the view as well. But the DisplayTemplate will change it for all other DateTime’s as well and you won’t have to concern yourself with it in the view since it happens for your automatically.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code like this: <% foreach (var item in Model) { %> <tr>
Hi I have some code like: foreach (var item in Model) { <div> @Html.DisplayFor(modelItem
Here is my razor code: <fieldset> <legend>Headline News</legend> @foreach (var item in Model) {
Why in the following Razor markup @foreach (var item in Model) { <tr> <td>@Html.DisplayFor(modelItem
I have the following code @foreach (var item in Model.Defaults) { <tr class=CertainCategory> <td>
I have the following code: foreach (var control in this.Controls) { } I want
I am experimenting with this code: foreach (var r in _vm.Rules.Take(20)) { Task.Factory.StartNew(() =>
My understanding is nothing will happen. For instance this code: foreach (var some in
I have some code that looks like this: foreach(var obj in collection) { try
Can you guys please check this code, it is MVC3 Razor <p><strong><u>Skills:</u></strong> @foreach (var

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.