?
Solved

convert an integer to a month name

Posted on 2005-04-04
9
Medium Priority
?
1,201 Views
Last Modified: 2008-03-17
I really need this by this morning because we are testing the program.  

I am trying to create a bunch of directories.   Right now it is creating a directory for the month with a corresponding number such as 1 for January instead of creating a folder called January.  I was wondering if there is any way that I can use either string.format or int.tostring to convert an integer number to the corresponding month.  I am also wondering if I can create custom formats and save them somewhere so that I would be able to use this format for future programs if I need it.

Thanks for you help.
0
Comment
Question by:fou99004
9 Comments
 

Author Comment

by:fou99004
ID: 13698963
This is a couple of things that I have tried, but neither is working.

//The value of j is 2, want month to be either Feb or February
string month = j.ToString("MMM");

string month = j.ToString();
string.Format("{0:MMM}",month);
0
 
LVL 10
ID: 13699224
Try this method

private String getMonth(int iMonth)
{
   String monthName=null;
   switch(iMonth)
  {
    case 1: monthName="January";
               break;
    case 2: monthName = "February";
                break;
    case 3: monthName = "March";
                break;
    case 4: monthName = "April";
                break;
    case 5: monthName = "May";
                break;
    case 6: monthName="June";
                break;
    case 7:  monthName="July";
                 break;
    case 8: monthName="August";
                break;
    case 9: monthName="September";
                break;
    case 10: monthName="October";
                  break;
    case 11: monthName="November";
                  break;
    case 12: monthName="December";
                  break;
     default: monthName=null;
                  break;
  return monthName;
  }
   
 }    

0
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 13699247
   string month = new date(2000, 2, 1).ToString("MMM")
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:fds_fatboy
ID: 13699275
Whoops I meant

   string month = new date(2000, j, 1).ToString("MMM");
0
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 13699319
Finally I'll get it right:
    string month = new DateTime(2000, j, 1).ToString("MMM");
0
 
LVL 10

Accepted Solution

by:
fds_fatboy earned 1000 total points
ID: 13699329
Of course for February instead of Feb you would use:

   string month = new DateTime(2000, j, 1).ToString("MMMM");
0
 

Author Comment

by:fou99004
ID: 13699759
Thanks fds fatboy,
     I was trying to do it without the DateTime variable, but it looks like I really don't have a choice.  I will wait to see if anybody else has a better solution, but I think that what you gave me is going to have to work.  Thanks for the help.

melchkishore,
    I didn't want to make a switch because I knew there was a better way, I just wasn't sure how to do it.  Thanks for the suggestion though.
0
 
LVL 2

Expert Comment

by:vinil
ID: 13701222
Are you looking for something like this:
static void Main(string[] args)
{
  int i = 2;
Console.WriteLine(System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.GetAbbreviatedMonthName(i));
}
To get the full month name, use GetMonthName method.

And, as you require the various month names etc. can be changed.
Take a look at this property DateTimeFormat.AbbreviatedMonthNames or DateTimeFormat.MonthNames

Hope that helps,
Regards,
Vinil
0
 

Expert Comment

by:fhbandy
ID: 13701321
Yes, i agree.... I would try a switch
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Make the most of your online learning experience.
Six Sigma Control Plans
Starting up a Project

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question