Display DateTime value in dd/mm/yyyy format in Asp.NET MVC

asp.net Mvcasp.net Mvc-4DatetimeDatetime Format

asp.net Mvc Problem Overview


Is it possible to display a DateTime value in dd/mm/yyyy format with the help of HTML Helper methods in Asp.NET MVC? I tried to do this by using some formats in @Html.LabelFor and adding some annotations to the related property like below but it does not make any sense. Any help to solve this problem would be appreciated.

Model:

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public Nullable<System.DateTime> RegistrationDate { get; set; }

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

All you have to do is apply the format you want in the html helper call, ie.

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

You don't need to provide the date format in the model class.

Solution 2 - asp.net Mvc

After few hours of searching, I just solved this issue with a few lines of code

Your model

      [Required(ErrorMessage = "Enter the issued date.")]
      [DataType(DataType.Date)]
      public DateTime IssueDate { get; set; }

Razor Page

     @Html.TextBoxFor(model => model.IssueDate)
     @Html.ValidationMessageFor(model => model.IssueDate)

Jquery DatePicker

<script type="text/javascript">
    $(document).ready(function () {
        $('#IssueDate').datepicker({
            dateFormat: "dd/mm/yy",
            showStatus: true,
            showWeeks: true,
            currentText: 'Now',
            autoSize: true,
            gotoCurrent: true,
            showAnim: 'blind',
            highlightWeek: true
        });
    });
</script>

Webconfig File

    <system.web>
		<globalization uiCulture="en" culture="en-GB"/>
	</system.web>

Now your text-box will accept "dd/MM/yyyy" format.

Solution 3 - asp.net Mvc

I know this is an older question, but for reference, a really simple way for formatting dates without any data annotations or any other settings is as follows:

@Html.TextBoxFor(m => m.StartDate, new { @Value = Model.StartDate.ToString("dd-MMM-yyyy") })

The above format can of course be changed to whatever.

Solution 4 - asp.net Mvc

Since the question was "display" :

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

Solution 5 - asp.net Mvc

Or just use this in your View(Razor page)

@item.ResgistrationhaseDate.ToString(string.Format("dd/MM/yyyy"))

I recommend that don't add date format in your model class

Solution 6 - asp.net Mvc

You need to use html helper, and you don't need to provide date format in model class. e.x :

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

Solution 7 - asp.net Mvc

It might be too late to answer this in 2019. but I tried all the answers and none worked for me. So I solved it simply this way:

@Html.EditorFor(m => m.SellDateForInstallment, "{0:dd/MM/yyyy}", 
               new {htmlAttributes = new { @class = "form-control", @type = "date" } })

EditorFor is what worked for me.

Note that SellDateForInstallment is a Nullable datetime object.

public DateTime? SellDateForInstallment { get; set; } // Model property

Solution 8 - asp.net Mvc

I made a global configuration in "Web.config" inside etiquet system web:

globalization uiCulture="en" culture="en-US

In CsHtml: @Html.TextBoxFor(model => model.fechaConfirmacion, new { @type = "date", @Value = Model.fechaConfirmacion.ToString("MM/dd/yyyy hh:mm:sszzz") })

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMurat YıldızView Question on Stackoverflow
Solution 1 - asp.net MvcMahajan344View Answer on Stackoverflow
Solution 2 - asp.net MvciYogiView Answer on Stackoverflow
Solution 3 - asp.net MvcDav.idView Answer on Stackoverflow
Solution 4 - asp.net MvcStevenView Answer on Stackoverflow
Solution 5 - asp.net MvcAymanView Answer on Stackoverflow
Solution 6 - asp.net MvcJehad87View Answer on Stackoverflow
Solution 7 - asp.net MvcRamy M. MousaView Answer on Stackoverflow
Solution 8 - asp.net MvcFreddy FernandezView Answer on Stackoverflow