Solved

Convert month to number

Posted on 2009-05-06
5
632 Views
Last Modified: 2013-11-26
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
Comment
Question by:lrbrister
5 Comments
 
LVL 5

Accepted Solution

by:
Buginator earned 500 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

by:oobayly
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;

Open in new window

0
 
LVL 8

Expert Comment

by:RyanAndres
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},

};
 

//	THEN ADD TO YOUR LISTBOX'S ITEMS

comboBox.Items.AddRange(months);
 

//	SET THE PROPERTIES IT SHOULD USE FOR DISPLAY/VALUE

//	THIS CAN BE SET IN THE DESIGNER PROPERTIES

comboBox.DisplayMember = "Name";

comboBox.ValueMember = "Number";

Open in new window

0
 
LVL 8

Expert Comment

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

Author Comment

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now