-3

I am trying to turn a month name string into an integer. From my form I am getting a string value for the month, "march" or "april". I was wondering if there was a way of using a library in order to get a number from 1 to 12 as a representative of the months value something like this:

int monthInDigit = DateTime.ParseExact("march", "MMMM", CultureInfo.CurrentCulture).Month;

this code gives me this error enter image description here

If not, the other solution I had in mind was for my form to save a integer value instead of the string name which seems to be the easiest solution at the moment. Still I would like to know what you think and if there is a way of doing this.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Lostaunaum
  • 697
  • 1
  • 10
  • 31
  • 1
    Minus one for not researching this. http://stackoverflow.com/questions/258793/how-to-parse-a-month-name-string-to-an-integer-for-comparison-in-c – rory.ap Jun 29 '15 at 20:39
  • That does not work I have tried multiple times you will have to create a library manually and thats what I want to avoid. @roryap – Lostaunaum Jun 29 '15 at 20:42
  • you're an `M` short in your code. should be `"MMMM"` - then it will work – Darren Wainwright Jun 29 '15 at 20:43
  • The whole library for one little function ?! – Disappointed Jun 29 '15 at 20:43
  • @Disappointed There are many ways to do this I want to find out whats the best most efficient way. Manually writing libraries when they exist is not the best answer. – Lostaunaum Jun 29 '15 at 20:45
  • Did you take a look at the suggestion to use a `Dictionary`? There's no need to create a library for this. – Tim Jun 29 '15 at 20:54
  • I'm not sure what is being asked here. Can you paste an example of the string in `creditCard.ExpirationMonth`? And aren't CC expirations in the format MM/YY ? – theMayer Jun 29 '15 at 20:54
  • 2
    Have you tried "March"? Month names are capitalized in the Engish language which is what I'm assuming your currentCulture is. – Eris Jun 29 '15 at 20:56
  • Here's a link to the valid names of months: https://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.monthnames%28v=vs.110%29.aspx – Eris Jun 29 '15 at 21:13

1 Answers1

1

According to MSDN DateTimeFormatInfo.MonthNames, "march" is not valid, it has to be capitalized.

A one-dimensional array of type String containing the culture-specific full names of the months. In a 12-month calendar, the 13th element of the array is an empty string. The array for InvariantInfo contains "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", and "". [emphasis mine]

In powershell, you can get this list using:

[System.Globalization.CultureInfo]::CurrentCulture.DateTimeFormat.MonthNames
Eris
  • 7,378
  • 1
  • 30
  • 45