Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8303661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:35:56+00:00 2026-06-08T17:35:56+00:00

Just curious… So I get that if I convert the string version of the

  • 0

Just curious…
So I get that if I convert the string version of the date to a DateTime object and pass it into the String.Format() method, then I”ll get the desired results.

String.Format("The date is {0:MMMM dd, yyyy}", DateTime.Parse("05-22-2012"));

“The date is May 22, 2012”

But why doesn’t this work?

String.Format("The date is {0:MMMM dd, yyyy}", "05-22-2012")

“The date is 05-22-2012”

Sorry if this is a stupid question, but I’m just trying to understand how this works.
Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T17:35:57+00:00Added an answer on June 8, 2026 at 5:35 pm

    The other answers here hit on salient points, but let’s put them all together an examine how String.Format works.

    It has five overloads, but we’re going to talk only about the one that they all redirect to (this is not the actual code, if you want to see it with Reflector or ILSpy, you will find it in StringBuilder.AppendFormat). This is simplified for easy understanding.

    public static string Format(IFormatProvider provider, string format, params object[] args)
    {
        StringBuilder sb = new StringBuilder();
    
        // Break up the format string into an array of tokens
        Token[] tokens = ParseFormatString(format);
    
        foreach (Token token in tokens)
        {
            switch (token.TokenType)
            {
                // A Text token is just some text to output directly
                case TokenType.Text:
                    sb.Append(token.Text);
                    break;
    
                // An Index token represents something like {0} or {2:format}
                //  token.Index is the argument index
                //  token.FormatText is the format string inside ('' in the first example, 'format' in the second example)
                case TokenType.Index:
                    {
                        object arg = args[token.Index];
    
                        IFormattable formattable = arg as IFormattable;
                        if (formattable != null && token.FormatText.Length > 0)
                        {
                            // If the argument is IFormattable we pass it the format string specified with the index
                            sb.Append(formattable.ToString(token.FormatText, provider));
                        }
                        else
                        {
                            // Otherwise we just use Object.ToString
                            sb.Append(arg.ToString());
                        }
                    }
                    break;
            }
        }
    
        return sb.ToString();
    }
    

    In your question you ask why the format string doesn’t get applied when you pass “05-22-2012”. As Guffa said, that is not a DateTime object, it is a String object.

    As GSerjo said, a String is not IFormattable. Strings are not formattable because formatting is the process of converting something into a String. A string is already a string!

    So you can see that when the Format method gets to indexer, arg will not be IFormattable and it will simply call ToString. Calling ToString on a string simply returns itself, it’s already a string.

    In summary, if your format string contains an index with an inner-format string (e.g. {0:format}), that inner-format string will only be applied if the associated argument is IFormattable and it knows what to do with the format string you give it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just curious, is there a format string I can use to output something like
Just curious how you would comment this line of code: string customerNm = customerNm.EndsWith(s)
Just curious. Using gcc/gdb under Ubuntu 9.10. Reading a C book that also often
Just curious, of the physical bits taken. I understand that the difference in size
Just curious. My thought is that generated files have negligible overhead in terms of
Just curious, I've noticed that some php files have extension 'class.php' or 'html.php'. What
Just curious: sure, we all know that the general case of type inference for
Just curious: I have to do a mini-CMS that allows users to add pages
Just curious, if django has already proven that auto-admin can be done well, why
Just curious, when I imported data from a spreadsheet with 519 lines into an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.