Lately I took part in several code reviews and I noticed that whenever people need to extract the month name from the datetime object many different solutions are presented. Some of the solutions were not very successful, so I decided to shed some light on this issue through an example.

These are the most common (and not so good) code patterns I came across.

the first example involved using a predefined array for it:

string[] months = { "Jan", "Feb", "Mar", "Apr", "May", "Jun"
            , "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

Whenever they wanted to “extract” the month, they would use this line:

string m = months[DateTime.Now.Month];

The second example is a bit more sophisticated and involved defining of an Enum:

public enum Months

The month would be extracted the following way:

Months m = (Months)DateTime.Now.Month;

Those methods just don’t work if you have a localized environment (different Languages and calendars). What I am about to show you is how to use string format to easily extract the date, time, year, month and anything you want in any format you want in a localized fashion.

all you need to do is provide the format in the Datetime.ToString(<Format>) function, like this:

DateTime.Now.ToString(“hh:mm:ss”) –> 12:00:00
DateTime.Now.ToString(“dd,MMM yy”) –> 27,Mar 08
DateTime.Now.ToString(“dd,MMM yyyy hh:mm”) –> 27,Mar 2008 12:00

to get the month name in three letters just do this:

DateTime.Now.ToString(“MMM”) –> Mar

The examples are endless, you can do whatever you want. To help you experiment and understand it here is a nice tool by Chris Sells that shows you what would be the outcome of you format string.

Remember that everything changes according to the culture info so be sure to pay attention to that when you use the tool ( you can select the culture info) and when you actually code. This operation is also considered slow, I think it is worth it.


Tags :

11 Responses to “A Better Way to Extract Data from DateTime”

  1. DevSlick

    Said on March 27, 2008 :

    MSDN documentation on DateTime format strings is helpful:

    I have noticed that many people do not know the format strings exist or how to use them effectively.

  2. Andy Norris

    Said on March 27, 2008 :

    Are there really people who use a string array of months in C#?

    Yikes. That seriously belongs on Daily WTF.

  3. Admin

    Said on March 27, 2008 :

    Devslick – Thanks for the helpfull addition

    Andy – Indeed there are :)
    I admitt I did it too in my “early years” but no more :)

  4. John S.

    Said on March 27, 2008 :

    Here’s the format strings in cheat sheet form:

  5. Pete

    Said on March 28, 2008 :

    Although I would never use above crazy patterns, I don’t agree with you. I see it the other way around, the array-solution is the only safe way to know that you get the month’s name in the language you want.

    I’m not from an english-speaking country, so when I put my stuff on an english-speaking server and tell it ToString(“MMM”) it will not show the text it did on my testing machine.

  6. Admin

    Said on March 28, 2008 :

    Pete – I get what you are saying, but you can set the thread cultureinfo to be the same as the cultureinfo on your test machine and then get the same results. Besides that, it is generaly highly recommended to have the same settings on your test and deployment machines.

  7. Pete

    Said on March 28, 2008 :

    The settings (in web.config) are the same, but that doesn’t change that the server has, for example, another culture info (which can be changed) or another .NET-version or other variables.

  8. Admin

    Said on March 28, 2008 :

    Pete – I understand, But you can make yourself a test machine that has the exact same settings as the deployment machine by using VmWare or VirtualPC. That way, when you are done coding you know everything will work just fine on the deployment machine

  9. Pete

    Said on March 30, 2008 :

    Right you are, I’m going to contact my web hosting-company right away and ask them for an image of their servers.

  10. Admin

    Said on March 30, 2008 :


    If they say yes, tell them i want one to :)

    I do agree that when Web Application + shared hosting is the matter, an array is the way to go. But, for Desktop application which has a requirement for localization, you must use the string format.

1 Trackback(s)

  1. Mar 28, 2008: Dew Drop - March 28, 2008 | Alvin Ashcraft's Morning Dew

Post a Comment