Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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; } 
like image 428
Murat Yıldız Avatar asked Aug 17 '13 12:08

Murat Yıldız


People also ask

How to display Date in dd mm yyyy format in ASP net?

Text=System. DateTime. Now. ToString("dd/MM/yyyy");

How to convert DateTime in MVC?

var date = DateTime. Parse(model. dateValue); var printInfo = string. Format("{0:MM/dd/yyyy HH:mm:ss}", date);


Video Answer


1 Answers

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.

like image 132
Mahajan344 Avatar answered Sep 24 '22 00:09

Mahajan344