Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting month name to integer

I did a quick search for this and was surprised not to find it anywhere.

Basically looking to convert full month names (January, September, etc) to the equivalent number that would be used in mm/dd/yyyy format.

I can put together my own array and pull it out accordingly, but there has to be a quick and straightforward method already. Right?

like image 669
Mercurybullet Avatar asked Sep 07 '10 20:09

Mercurybullet


People also ask

How do I convert month name to number in Excel?

An alternative way to get a month number from an Excel date is using the TEXT function: =TEXT(A2, "m") - returns a month number without a leading zero, as 1 - 12. =TEXT(A2,"mm") - returns a month number with a leading zero, as 01 - 12.

How do I convert a month to a month number in Excel?

Please do as follows: Select a blank cell next to the sales table, type the formula =TEXT(A2*29,"mmm") (Note: A2 is the first number of the Month list you will convert to month name), and then drag the AutoFill Handle down to other cells. Now you will see the numbers (from 1 to 12) are converted to normal month names.


2 Answers

Dim monthName = "September"
Dim monthNumber = DateTime.ParseExact(monthName, "MMMM", CultureInfo.CurrentCulture).Month

If you are basing this on user input, I think this is the cleanest (especially if you are expecting multiple cultures to use this). DateTime.ParseExact will allow you to input any sort of input and translate it into a DateTime, then pluck off whatever part of it you care about.

If, however, this isn't have on user input, I would have to suggest using some sort of static collection (whether a dictionary or an enum).

like image 60
bdukes Avatar answered Sep 27 '22 15:09

bdukes


This is old but something I was looking for, just incase someone else comes looking I came up with an easy solution....

Dim sM as String = "Jun"
Dim iM as Byte = 0

iM = Month(sM & " 1, 2020")

iM will equal 6

like image 21
user10067059 Avatar answered Sep 27 '22 17:09

user10067059