Click here to Skip to main content
11,708,721 members (74,902 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: ASP.NET MVC
Hi all,
How to convert date format(dd/MM/yyyy) in MVC3 in foreach loop Table(Same as gridview)?

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

Sampath bejugama
Posted 29-Feb-12 0:47am
Edited 29-Feb-12 3:16am
Anuja Pawar Indore at 29-Feb-12 9:31am
Why repost
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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

@item.created_date.ToString("MMM dd yyy")
sampath1750 at 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 at 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 at 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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

in your model put something like this on the top of your Date..

[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
sampath1750 at 2-Mar-12 0:15am
i want to display the dd/MM/yyyy date in Textbox in foreach gridview after click edit button
botskie at 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 at 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 at 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 at 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.
sampath1750 at 5-Mar-12 2:17am
Top of the model i wrote [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0: d}")] but not working
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

Use This

@Html.TextBoxFor(m => m.MktEnquiryDetail.CallbackDate, "{0:dd/MM/yyyy}")
Rate this: bad
Please Sign up or sign in to vote.

Solution 5

plz chk this link to nice answer

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

  Print Answers RSS
0 Maciej Los 235
1 Sergey Alexandrovich Kryukov 189
2 Mika Wendelius 180
3 OriginalGriff 143
4 Peter Leow 130
0 OriginalGriff 9,253
1 Sergey Alexandrovich Kryukov 8,562
2 CPallini 5,189
3 Maciej Los 4,966
4 Mika Wendelius 3,856

Advertise | Privacy | Mobile
Web04 | 2.8.150819.1 | Last Updated 4 Feb 2014
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100