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.
精彩评论