Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# is there a easy way to convert month integer to name

I am trying to create a archive list for a blog application. I have a veiw model which has this code snippet:

@model IEnumerable<NPLHBlog.Models.ArchiveListModels>
...
        @foreach (var item in Model)
        {
            <li>@item.ArchiveMonth/@item.ArchiveYear : @item.PostCount</li>
        }
...

I am trying to print the item.ArchiveMonth as 'Jan' instead of '1'.

the ArchiveListModel is as follows:

public class ArchiveListModels
{
    public int ArchiveYear { get; set; }
    public int ArchiveMonth { get; set; }
    public int PostCount { get; set; }
}

and blogs are read from repository as follows:

    public IQueryable<ArchiveListModels> ArchiveList()
    {
        var archiveList = from blogs in db.Blogs
                          group blogs by new { blogs.PublishDate.Year, blogs.PublishDate.Month }
                              into dateGroup
                              select new ArchiveListModels()
                              {
                                  ArchiveYear = dateGroup.Key.Year,
                                  ArchiveMonth = dateGroup.Key.Month,
                                  PostCount = dateGroup.Count()
                              };
        return archiveList;
    }

Many thanks.

like image 769
Tripping Avatar asked Nov 30 '25 05:11

Tripping


1 Answers

CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(1);

or for abrev

CultureInfo.CurrentUICulture.DateTimeFormat.AbbreviatedMonthNames[1];
like image 168
burning_LEGION Avatar answered Dec 02 '25 18:12

burning_LEGION



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!