开发者

DateTimeFormatInfo.InvariantInfo or null in ToString / TryParseExact

开发者 https://www.devze.com 2023-01-15 21:21 出处:网络
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:

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:

conversionRe开发者_JAVA技巧sult.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ł


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消