• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 17850
  • Last Modified:

convert month name to month number in C#

Hello experts,

                   I am using a label to get the month name(Jan, Feb, Mar.....) using this line of code.
if (month > 12) month = month - 12;
        lblMonth.Text = String.Format("{0:MMMM}", DateTime.Parse(month.ToString() + "/01/1900"));

Now, how would I compare the month name in a label to the number(like 1 for Jan, 2 for Feb, 3 for Mar)..I just want to make some textboxes readonly by comparing the current month with the month name in the label.

I tried;
if (DateTime.Now.Month == Convert.ToDate(lblMonth.Text)
{
    //then do this
}

But I am not doing the casting right. Could someone please help?

                   
0
siddhuoops
Asked:
siddhuoops
1 Solution
 
dstanley9Commented:
try

if (DateTime.Now.Month == Convert.ToDate(lblMonth.Text + " 01, 1900").Month)
{
    //then do this
}

0
 
siddhuoopsAuthor Commented:
Cool..that worked. Thanks.
0
 
Jai STech ArchCommented:
hi, i just thought giving a vs2008 version of this
System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.MonthNames[2].ToString()
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now