Solved

convert month name to month number in C#

Posted on 2007-03-22
3
16,867 Views
Last Modified: 2010-05-19
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
Comment
Question by:siddhuoops
3 Comments
 
LVL 25

Accepted Solution

by:
dstanley9 earned 500 total points
Comment Utility
try

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

0
 

Author Comment

by:siddhuoops
Comment Utility
Cool..that worked. Thanks.
0
 
LVL 14

Expert Comment

by:jaiganeshsrinivasan
Comment Utility
hi, i just thought giving a vs2008 version of this
System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.MonthNames[2].ToString()
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now