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

Is it possible to display a DateTime value in dd / mm / yyyy format using the HTML Hepler in Asp.NET MVC ? I tried to do this using some formats in @Html.LabelFor and adding some annotations to the related property as shown below, but that doesn't 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; } 
+65
datetime asp.net-mvc asp.net-mvc-4 datetime-format
Aug 17 '13 at 12:12
source share
7 answers

All you have to do is apply the desired format in the html call, i.e.

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

You do not need to specify a date format in the model class.

+81
Aug 18 '13 at 16:05
source share

After hours of searching, I just solved this problem 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> 

Your text box will now take the format "dd / MM / yyyy .

+50
Sep 01 '13 at 12:54 on
source share

I know this is an older question, but for reference a very simple way to format 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, of course, can be changed to any.

+10
Dec 23 '14 at 13:25
source share

Or just use it on the View page (Razor)

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

I recommend not adding the date format to the model class

+7
Apr 14 '17 at 13:39 on
source share

Since the question was the "display":

 @Html.ValueFor(model => model.RegistrationDate, "{0:dd/MM/yyyy}") 
+6
Mar 13 '17 at 21:23
source share

You need to use the html helper and you don't need to specify the date format in the model class. ex:

 @Html.TextBoxFor(m => m.ResgistrationhaseDate, "{0:dd/MM/yyyy}") 
+2
Feb 21 '16 at 21:02
source share

It may be too late to answer this question in 2019 . but I tried all the answers and none worked for me. So I solved it just like this:

 @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 
0
Sep 07 '19 at 16:33
source share



All Articles