I am working on MVC 4.0./C#/Razor view. In model I have a date
[Display(Name = "For Period")]
[DisplayFormat(DataFormatString = "{0:dd-MMM-yyyy}")]
public System.DateTime ForPeriod { get; set; }
and in View I am getting date using
@Html.DisplayFor(modelItem => item.ForPeriod)
and in result I am getting date in "12-May-2015" format. I want to skip the day from this date and in result I want "May-2015".
My question is what should I used in View to get this "MMM-yyyy" format?
If you only want to display a date with the year and month, you can simply apply the custom number format "yyyymm" to the date(s). This will cause Excel to display the year and month together, but will not change the underlying date.
Select the cells you want to format. Press CTRL+1. In the Format Cells box, click the Number tab. In the Category list, click Date, and then choose a date format you want in Type.
First, pick the cells that contain dates, then right-click and select Format Cells. Select Custom in the Number Tab, then type 'dd-mmm-yyyy' in the Type text box, then click okay.
[Display(Name = "For Period")]
[DisplayFormat(DataFormatString = "{0:MMM-yyyy}")]
public System.DateTime ForPeriod { get; set; }
More information on formatting dates in C# can be found on MSDN here.
There is an additional option of using a custom editor template which you can read this article on stack overflow for more details - ASP.NET MVC 4 Editor Template for basic types
You can format date in View file by following way
<span>@Convert.ToDateTime(item.EffectiveDate).ToString("MMM-yyyy")</span>
For can also format date from model
[Display(Name = "For Period")]
[DisplayFormat(DataFormatString = "{0:MMM-yyyy}")]
public System.DateTime ForPeriod { get; set; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With