?
Solved

convert an integer to a month name

Posted on 2005-04-04
9
Medium Priority
?
1,191 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

752 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