5

I want to return the number of a month and i made a function but it always returns 0

this is my code:

public int getNrMonth(String s)
    {
        int nr=0;
        if (s.Equals("January"))
            nr = 1
        if (s.Equals("February"))
            nr = 2;
        return nr;


    }

Could someone tell me wath is wrong please? I'm beginner!

Matthew Jones
  • 25,644
  • 17
  • 102
  • 155
john
  • 51
  • 1
  • Are you trying to compare "january" with "January"? – Gabe May 19 '10 at 17:52
  • 1
    It's not an answer to your problem but you should take a look at DateTimeFormatInfo.MonthNames which return a string array of the Month for the current culture. Then you can use the index to get your month number from your month name :http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.monthnames%28v=VS.71%29.aspx – JoeBilly May 19 '10 at 18:18

5 Answers5

12

Why wouldn't you use the built in function:

DateTime.ParseExact(monthName, "MMMM", CultureInfo.CurrentCulture ).Month

Here is an example on use:

How to parse a month name (string) to an integer for comparison in C#?

Community
  • 1
  • 1
Avitus
  • 15,640
  • 6
  • 43
  • 53
  • I think you're confused? `Month.toInt` is not a built-in function. The OP in the question you link to was asking if there's anything *like* that; the actual accepted answer suggests using `DateTime.ParseExact`. – Dan Tao May 19 '10 at 17:56
  • There is no `Month.toInt` operation. You're looking for the `DateTime.Month` property. – Jeff Yates May 19 '10 at 17:57
  • Typing at too many things sorry for the confusion. I have edited to be correct. – Avitus May 19 '10 at 17:58
  • Uh ? There's no such method ! – JoeBilly May 19 '10 at 17:58
5

It'd be better to do it like this:

switch (s.Trim().ToUpper())
{
    case "JANUARY": return 1;
    case "FEBRUARY": return 2;
    // etc.
}

return 0;

Reasons:

  1. switch is optimized to begin with (small point, but worth mentioning).
  2. Once you have the value, all the remaining if checks are pointless.
  3. Assuming you want "january" and "January" and " January " and "jaNuarY" all to return 1, the Trim() and ToUpper() calls will take care of that.
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
1

OK, you're a beginner, but you still have tools at your disposal. Set a breakpoint and step through in the debugger. Take a look at the value of s and nr as you do. Notice which if statements execute the nr = part and which you don't. Then you will understand. As it stands I don't think you pasted your real code in, because your question is missing a semi colon and might not even compile.

Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
0

Why do it the simple, easy way when you can do it the long, complicated LINQ way!

    int GetMonthNumber(string month)
    {
        return System.Globalization.DateTimeFormatInfo.CurrentInfo.MonthNames
            .Select((m, i) => new { Month = m, Number = i + 1 })
            .First(m => m.Month.ToLower() == month.ToLower())
            .Number;
    }
Despertar
  • 21,627
  • 11
  • 81
  • 79
0

Try this sample:

     string value = "June";
     DateTime result;
     bool ok;
     ok = DateTime.TryParseExact(value, "MMMM",
                    CultureInfo.CurrentCulture, DateTimeStyles.None, out result);
     if ( ok )
     {
        int monthNumber = result.Month;
        Console.WriteLine(monthNumber);
     }
Zamboni
  • 7,897
  • 5
  • 43
  • 52