Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Convert month to number

Posted on 2009-05-06
Medium Priority
651 Views
In this code I need to pass in an integer instead of the "Mmm" that is the dynamically populated value

arriveMonth.SelectedValue  right now = "jun" etc

I need it to = "6"

Can I cas or convert this?
0
Question by:lrbrister
[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

LVL 5

Accepted Solution

Buginator earned 2000 total points
ID: 24318435
Hi,

String monthName = arriveMonth.SelectedValue.ToString();
String monthNumber;

if ((monthName .Equals("January")) monthNumber =1;
else if ((monthName .Equals("February")) monthNumber =2;
else if ((monthName .Equals("March")) monthNumber =3;
else if ((monthName .Equals("April")) monthNumber =4;
else if ((monthName .Equals("May")) monthNumber =5;
else if ((monthName .Equals("Juny")) monthNumber =6;
else if ((monthName .Equals("July")) monthNumber =7;
else if ((monthName .Equals("August")) monthNumber =8;
else if ((monthName .Equals("September")) monthNumber =9;
else if ((monthName .Equals("October")) monthNumber =10;
else if ((monthName .Equals("November")) monthNumber =11;
else if ((monthName .Equals("Desember")) monthNumber =12;
0

LVL 15

Expert Comment

ID: 24318473
Either use a switch (Select Case in VB.Net) statement. The other option is to do this:
``````string monthText = "jun";
int month = DateTime.ParseExact(
string.Format("01 {0} 2000", monthText),
"dd MMM yyyy",
System.Globalization.DateTimeFormatInfo.Current).Month;
``````
0

LVL 8

Expert Comment

ID: 24318720
How about managing them more explicitly?
``````//	CREATE MONTH CLASS
public class Month
{
public string Name { get; set; }
public int Number { get; set; }
}

//	CREATE MONTH OBJECTS ONCE
Month[] months = new []
{
new Month{Name = "Jan", Number = 1},
new Month{Name = "Feb", Number = 2},
new Month{Name = "Mar", Number = 3},
new Month{Name = "Apr", Number = 4},
new Month{Name = "May", Number = 5},
new Month{Name = "Jun", Number = 6},
new Month{Name = "Jul", Number = 7},
new Month{Name = "Aug", Number = 8},
new Month{Name = "Sep", Number = 9},
new Month{Name = "Oct", Number = 10},
new Month{Name = "Nov", Number = 11},
new Month{Name = "Dec", Number = 12},
};

//	SET THE PROPERTIES IT SHOULD USE FOR DISPLAY/VALUE
//	THIS CAN BE SET IN THE DESIGNER PROPERTIES
comboBox.DisplayMember = "Name";
comboBox.ValueMember = "Number";
``````
0

LVL 8

Expert Comment

ID: 24318745
Using my solution, arriveMonth.SelectedValue will return 6
0

Author Comment

ID: 24318798
RyanAndres:
Must have got caught between page refreshes.  I accepted while you were typing probably
0

## Featured Post

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
###### Suggested Courses
Course of the Month12 days, 1 hour left to enroll