tags:

views:

33

answers:

1

Hi,

I'm wondering what is the difference for application between those two calls, in means of resulting DateTime object, whether TryParseExact will succeed, and result of ToString operation:

DateTime.TryParseExact(value, "MMM yyyy", DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out conversionResult)

DateTime.TryParseExact(value, "MMM yyyy", null, DateTimeStyles.None, out conversionResult)

and those two calls:

conversionResult.ToString("d M yyyy", DateTimeFormatInfo.InvariantInfo)

conversionResult.ToString("d M yyyy")

I know that providing null will cause usage of thread culture info...

I just can't work out what is the reason for specyfing Culture info when I explicitly provide format, without separators etc...

Thanks, Paweł

+1  A: 

What month names would you expect it to use, just as an example of how culture can make a difference?

Sure, in some formats that won't make a difference, but in others it will. For example:

using System;
using System.Globalization;
using System.Threading;

class Test
{
    static void Main()
    {
        CultureInfo french = new CultureInfo("fr-FR");
        Thread.CurrentThread.CurrentCulture = french;
        DateTime now = DateTime.Now;
        Console.WriteLine(now.ToString("d MMM yyyy"));
        Console.WriteLine(now.ToString("d MMM yyyy",
                                       CultureInfo.InvariantCulture));
    }
}

Results:

14 sept. 2010
14 Sep 2010

(And that's a relatively tame case.)

When you parse or format a string, you need to consider whether the source/target is a computer or a user. If it's a computer, the invariant culture is probably most appropriate. If it's a user, their culture is probably the most appropriate.

Jon Skeet
English abbreviated name of the month. And app culture is set to en-GB. Now I get one point, if I had months in different language TryParseExact would fail. But appart from that?
dragonfly
@dragonfly: Well if you know that the current culture is en-GB there won't be very many differences... and if the current culture were en-US there'd be even fewer differences... but do you really want to rely on that? Make your code express what you're trying to do - whether you're trying to parse/format for a computer or a user.
Jon Skeet
Thanks for clarifying
dragonfly