Click here to Skip to main content
12,898,858 members (54,396 online)
Rate this:
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
Updated 29-Feb-12 3:16am
Anuja Pawar Indore 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 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.
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 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.
sampath1750 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
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170424.1 | Last Updated 4 Feb 2014
Copyright © CodeProject, 1999-2017
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