Click here to Skip to main content
15,886,830 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hi all,
How to convert date format(dd/MM/yyyy) in MVC3 in foreach loop Table(Same as gridview)?

My code is as follows
C#
@Html.DisplayFor(modelItem => item.created_date) 

Thanks,
Sampath bejugama
Posted
Updated 29-Feb-12 3:16am
v2
Comments
Anuja Pawar Indore 29-Feb-12 9:31am    
Why repost

Instead of using DisplayFor use this in a div(or any other control)

C#
@item.created_date.ToString("MMM dd yyy")
 
Share this answer
 
Comments
sampath1750 29-Feb-12 8:11am    
How can i show short date in textbox
My textbox code is @Html.EditorFor(model => model.created_date)
Syed Salman Raza Zaidi 29-Feb-12 9:05am    
Save it in a string, like string abc=item.created_date.ToString("MMM dd yyy");
and then make textbox like Html.TextBox("id",abc); this is done on your view
sampath1750 29-Feb-12 23:55pm    
Hi Syed,My text box code in view

@Html.EditorFor(model => model.created_date)

how can i write like string. string datatype is not accepting in view

i am used this in Foreach loop table edit case.
in your model put something like this on the top of your Date..

C#
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString ="{0: d}")]
        [Required(ErrorMessage = "Date is required.")]
        public DateTime Date { get; set; }



tell me if you want to display the date automatically as you create the item with it.. and I will teach you how
 
Share this answer
 
v2
Comments
sampath1750 2-Mar-12 0:15am    
i want to display the dd/MM/yyyy date in Textbox in foreach gridview after click edit button
botskie 2-Mar-12 3:31am    
is that something like that you want to edit your date in your edit view?
where is that foreach gridview can be found exactly? is it in your index or edit in view? coz im a little confuse on your statement.
sampath1750 2-Mar-12 4:21am    
In foreach table(Gridview) after clicking edit button go to edit page, in that page i need created_Date textbox date will be dd/MM/YYYY date format
botskie 2-Mar-12 4:42am    
are you trying to remove the time on the datetime and just want date on the edit view? or i have misinterpret your question.
botskie 2-Mar-12 4:54am    
coz the first solution that I gaved to you work on me. The datetime format "12/23/2011 12:55:55 PM" on my edit changed to "12/23/2011" when i put [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString ="{0: d}")] on the top of its model.
Use This

@Html.TextBoxFor(m => m.MktEnquiryDetail.CallbackDate, "{0:dd/MM/yyyy}")
 
Share this answer
 
v2
http://dpusdc.blogspot.in/

plz chk this link to nice answer
 
Share this answer
 
Comments
Saravana Kumar Thangavelu 9-Feb-18 6:28am    
No use

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900