Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Can we change date formate in cshtml file in MVC 4.0?
If yes then how can we change the date formate to dd MMMM yyyy(01 Aug 2014).
Please suggest.

Thank you,

Regards,
Ashish
Posted

you can code as
@DateTime.Now.ToString("dd MMM yyyy")


or

C#
@Convert.ToDateTime("01-09-2014").ToString("dd MMM yyyy")
 
Share this answer
 
Comments
Aashish vermaa 1-Sep-14 8:03am    
I am showing data using foreach loop on the cshtml page like this:
<tr>
<td>Last Update 
@Html.DisplayFor(modelItem => Convert.ToDateTime(item.lastUpdate).ToString("dd MMM yyyy"))
</td>
</tr>

But getting the following error while using this code:
Templates can be used only with field access, property access, single-dimension array index, or single-
parameter custom indexer expressions.

Please suggest some solution.
item.lastUpdate.ToString("dd MMM yyyy")

How about below line

@Html.DisplayFor(m=>m.lastUpdate).ToString("dd MMM yyyy"))
 
Share this answer
 
v2
Comments
Aashish vermaa 1-Sep-14 8:42am    
now I am getting this error message:
No overload for method 'ToString' takes 1 arguments
Ranjeet Patel 1-Sep-14 8:53am    
item.lastUpdate.ToString("MM/dd/yyyy") and check if this is correct
Ranjeet Patel 1-Sep-14 8:48am    
did you try upper row?
Aashish vermaa 1-Sep-14 8:53am    
No overload for method 'ToString' takes 1 arguments

<tr>
<td>Last Update 
@item.lastUpdate.ToString("dd MMMM yyyy")
Ranjeet Patel 1-Sep-14 8:59am    
@item.lastUpdate.ToString("MM/dd/yyyy") try this line once
I know the formate would be different but at least try this, then you can think on the date format

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