Display DateTime value in dd/mm/yyyy format in Asp.NET MVC
Solution 1:
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:
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:
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.