Click here to Skip to main content
13,192,426 members (48,008 online)
Click here to Skip to main content
Add your own
alternative version

Stats

93.2K views
4 bookmarked
Posted 13 Jun 2012

Get Month Name from Month Number or vice versa

, 13 Jun 2012
Rate this:
Please Sign up or sign in to vote.
Get Month Name from Month Number or vice versa

We need a query to display month name and we have month number for that. Then firstly we create a switch case statement of if else statement for that. But we can get that easily by using the below code ..

int iMonthNo = 3; 
DateTime dtDate = new DateTime(2000, iMonthNo, 1); 
string sMonthName = dtDate.ToString("MMM"); 
string sMonthFullName = dtDate.ToString("MMMM"); 

And as well as if we have full name or half name of month and want to get month number, then we can use the code below ..

string sMonthName = "Jan"; 
sMonthName = "January"; 
int iMonthNo = Convert.ToDateTime("01-" + sMonthName + "-2011").Month; 

This is an optimized process for that in place of conditional statements..

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Technoses
Software Developer (Senior) Super Eminent Softwares
India India
This member doesn't quite have enough reputation to be able to display their biography and homepage.
Group type: Organisation

42 members


You may also be interested in...

Comments and Discussions

 
SuggestionThis is not "optimal" but it is fast, and can throw an exception. Pin
Matt T Heffron13-Jun-12 12:36
memberMatt T Heffron13-Jun-12 12:36 
GeneralRe: This is not "optimal" but it is fast, and can throw an exception. Pin
BCantor13-Jun-12 12:59
memberBCantor13-Jun-12 12:59 
GeneralRe: This is not "optimal" but it is fast, and can throw an exception. Pin
Matt T Heffron13-Jun-12 13:11
memberMatt T Heffron13-Jun-12 13:11 
GeneralRe: This is not "optimal" but it is fast, and can throw an exception. Pin
Technoses13-Jun-12 19:12
groupTechnoses13-Jun-12 19:12 
GeneralRe: This is not "optimal" but it is fast, and can throw an exception. Pin
cansino5-Jul-12 21:31
membercansino5-Jul-12 21:31 
QuestionExtension Methods Pin
Matt U.13-Jun-12 6:53
memberMatt U.13-Jun-12 6:53 
The ideas can easily be placed into extension methods:

    public static class DateTimeExt
    {
        public static string GetShortMonthName(this DateTime dateTime)
        {
            return dateTime.ToString("MMM");
        }
 
        public static string GetFullMonthName(this DateTime dateTime)
        {
            return dateTime.ToString("MMMM");
        }
    }


Big Grin | :-D
djj55: Nice but may have a permission problem
Pete O'Hanlon: He has my permission to run it.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171017.2 | Last Updated 13 Jun 2012
Article Copyright 2012 by Technoses
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid