Solved

convert month name to month number in C#

Posted on 2007-03-22
3
17,064 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
ID: 18772302
try

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

0
 

Author Comment

by:siddhuoops
ID: 18772347
Cool..that worked. Thanks.
0
 
LVL 14

Expert Comment

by:Jai S
ID: 20821150
hi, i just thought giving a vs2008 version of this
System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.MonthNames[2].ToString()
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

808 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