fou99004
asked on
convert an integer to a month name
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.
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.
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;
}
}
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;
}
}
string month = new date(2000, 2, 1).ToString("MMM")
Whoops I meant
string month = new date(2000, j, 1).ToString("MMM");
string month = new date(2000, j, 1).ToString("MMM");
Finally I'll get it right:
string month = new DateTime(2000, j, 1).ToString("MMM");
string month = new DateTime(2000, j, 1).ToString("MMM");
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
Are you looking for something like this:
static void Main(string[] args)
{
int i = 2;
Console.WriteLine(System.T hreading.T hread.Curr entThread. CurrentCul ture.DateT imeFormat. GetAbbrevi atedMonthN ame(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.Abbreviated MonthNames or DateTimeFormat.MonthNames
Hope that helps,
Regards,
Vinil
static void Main(string[] args)
{
int i = 2;
Console.WriteLine(System.T
}
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.Abbreviated
Hope that helps,
Regards,
Vinil
Yes, i agree.... I would try a switch
ASKER
//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}",mo