I have below entity in a mvc model:
[Display(Name = "Date Of Birth")]
public DateTime? DateOfBirth { get; set; }
and I am using it in a view as:
<td title="@u.CreatedDate">
@if (@u.DateOfBirth != null)
{
@u.DateOfBirth
}
</td>
This is working piece of code but I want to show this date in dd/mm/yyyy format.
I have tried something like this:
1. string.Format("dd/MM/yyyy", u.DateOfBirth);
2. [Display(Name = "Date Of Birth")]
[DisplayFormat (DataFormatString="dd/MM/yyyy")]
public DateTime? DateOfBirth { get; set; }
But nothing works fine for me. Please help
3 options you can use
@Html.DisplayFor(m => u.DateOfBirth) // assumes you have [DisplayFormat(DataFormatString="{0:dd/MM/yyyy}")]
@String.Format("{0:dd/MM/yyyy}", u.DateOfBirth)
@u.DateOfBirth.Value.ToString("dd/MM/yyyy")
In the first 2 cases, you do not need the @if (@u.DateOfBirth != null)
check