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?
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.
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.
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).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With