Click here to Skip to main content
15,886,760 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
My requirements is i have one text box if user enter 4 in textbox then it should be display 'April' in label means i want to convert number into month only
how can i acheive this.

Thanks in Advance
Posted
Updated 16-Jan-12 22:30pm
v3

C#
private static string GetMonthName(int month)  
{     
 
DateTime date = new DateTime(2010, month, 1);
return date.ToString("MMMM");  
 
}
 
Share this answer
 
C#
DateTime strDate = New DateTime(2000, monthNum, 1); // monthNum is your input
strDate.ToString("MMMM"); // this is the month name
 
Share this answer
 
v4
Comments
Dalek Dave 17-Jan-12 4:36am    
Dammit man, beat me too it by a few seconds
sriman.ch 17-Jan-12 4:48am    
Good one my 5!
bbirajdar 17-Jan-12 5:59am    
Its not the solution. Its returns the month name as 'Apr' due to ("MMM"); and NOT as 'April' as asked above. On the contrary, I have have the correct solution here http://www.codeproject.com/Answers/315835/Convert-Number-into-month-text#answer4

And (sigh) it was downvoted. :(

Prerak , you need to correct MMM to MMMM
Prerak Patel 17-Jan-12 6:44am    
Thanks, and 5 to your answer.
C#
string strMonth = new DateTime(1900,month,01).ToString("MMMM");
 
Share this answer
 
v2
Comments
bbirajdar 17-Jan-12 6:01am    
returns month name as 'Apr' and not as 'April' as required. Change MMM to MMMM
C#
class Program
    {
        static void Main(string[] args)

        {
            if (GetMonth(5) != string.Empty)
            {
                Console.WriteLine(GetMonth(5));
            }
            else
            {
                Console.WriteLine("Invalid");
            }

            Console.ReadLine();
        }

        private static string GetMonth(int month)
        {
            try
            {
                DateTime date = new DateTime(1900, month, 1);

                return date.ToString("MMM");

            }
            catch (Exception ex)
            {

                return string.Empty;
            }
        }
 
Share this answer
 
Comments
CPallini 17-Jan-12 4:58am    
Don't know why someone downvoted you, have my 5.
ambarishtv 17-Jan-12 5:00am    
Thank you..
bbirajdar 17-Jan-12 6:07am    
Its not the solution. Its returns the month name as 'Apr' due to ("MMM"); and NOT as 'April' as asked above. On the contrary, I have have the correct solution here http://www.codeproject.com/Answers/315835/Convert-Number-into-month-text#answer4

And (sigh) it was downvoted. :(
Something like this:
C#
DateTime dt = new DateTime(1, Int32.Parse(myTextBox.Text), 1);
mylabel.Text = dt.ToString("MMMM");


?
 
Share this answer
 
Comments
bbirajdar 17-Jan-12 6:00am    
100% correct solution.. My vote of 5
[no name] 17-Jan-12 6:44am    
Thanks for comment. updated

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