Tech Off Post

Single Post Permalink

View Thread: C# Sting converstion
  • User profile image
    the_corona

    Sven Groot said:
    the_corona said:
    *snip*

    That's not necessary. If you want to be culture independent, just use the invariant culture:

     

    string filename = string.Format(CultureInfo.InvariantCulture, @"D:\logs\{0:ddMMyyyy}.log", myDate)

     

    This would have the same general effect as your code, without having to do manual date formatting.

    ah, yes thats interesting, though I've never seen this before. I have to wonder though why you need to specify culturinvariant in that example given you very specifically ask for ddMMyyyy ?

     

    But come to think of it, Date.ToString I think also has an overload to do the formating the way you want, so even something like myDate.ToString("ddMMYYYY") - (I'm not sure of the exact syntax) - could get you a culture invariant string.

     

    Thinking about this more. does the {0:xx} in string.format end up as a string parameter to the first argument's ToString(string format)

    overload? So it ends up like

    string.format("{0:xx}", myDate) == string.format("{0}", myDate.ToString("xx"));

    Does this work in general or only with DateTime?