Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NETMVC
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) 

Thanks,
Sampath bejugama
Posted 29-Feb-12 0:47am
Edited 29-Feb-12 3:16am
v2
Comments
Anuja Pawar Indore at 29-Feb-12 9:31am
   
Why repost
Rate this: bad
good
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")
  Permalink  
Comments
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
good
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
  Permalink  
v2
Comments
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
good
Please Sign up or sign in to vote.

Solution 4

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

Solution 5

http://dpusdc.blogspot.in/
 
plz chk this link to nice answer
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Shai Vashdi 1,518
1 OriginalGriff 478
2 Manas Bhardwaj 394
3 Tadit Dash 310
4 Damith Weerasinghe 260
0 Sergey Alexandrovich Kryukov 9,575
1 OriginalGriff 5,856
2 Peter Leow 4,405
3 Maciej Los 3,540
4 Abhinav S 3,513


Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 4 Feb 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid