Best way to turn an integer into a month name in c# ?

Best way to turn an integer into a month name in c# ?

Asked on December 17, 2018 in C#.
Add Comment


  • 6 Answer(s)

    This is a great fast solution

    CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(1);
    

    Refer this for get monthname in date time format.

    Answered on December 17, 2018.
    Add Comment

    Here is the best solution , and this is modernized with the accurate object with name space.

    //This was wrong
    //CultureInfo.DateTimeFormat.MonthNames[index];
     
    //Correct but keep in mind CurrentInfo could be null
    DateTimeFormatInfo.CurrentInfo.MonthNames[index];
    
    Answered on December 17, 2018.
    Add Comment

    We can simply use this,

    somedatetime.ToString("MMMM")?
    
    Answered on December 17, 2018.
    Add Comment

    You can use a static method from the Microsoft.VisualBasic namespace:

    string monthName = Microsoft.VisualBasic.DateAndTime.MonthName(monthInt, false);
    Answered on January 15, 2019.
    Add Comment

    We need a query to display month name and we have month number for that. Then firstly we create a switch case statement of if else statement for that. But we can get that easily by using the below code ..

    int iMonthNo = 3; 
    DateTime dtDate = new DateTime(2000, iMonthNo, 1); 
    string sMonthName = dtDate.ToString("MMM"); 
    string sMonthFullName = dtDate.ToString("MMMM");

    And as well as if we have full name or half name of month and want to get month number, then we can use the code below ..

    string sMonthName = "Jan"; 
    sMonthName = "January"; 
    int iMonthNo = Convert.ToDateTime("01-" + sMonthName + "-2011").Month;

    This is an optimized process for that in place of conditional statements..

    Answered on January 15, 2019.
    Add Comment

    GetMonthName from DateTimeFormatInfo

    http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.getmonthname.aspx

    You can do it by:

    CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(1)

    Answered on February 9, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.