Seeing a strange issue where on some systems the below code steps into the if statement (i.e. it returns true) while in other systems it returns false and steps into the else statement. What environmental conditions or framework version changes am I missing where this was changed? For example .net Fiddle returns true, but my own console apps return false.
DateTime time;
formatText = "";
if (DateTime.TryParse (DateTime.Now.ToString(formatText), CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out time))
{
// If TryParseExact Worked
Console.WriteLine ("True: " + time.ToString ());
}
else
{
// If TryParseExact Failed
Console.WriteLine ("Failed to Parse Date");
}
String representations of DateTime
are culture specific.
Passing an empty string or null
as the format
parameter of the ToString
overload of DateTime
is the same as passing the standard format specifier "G"
- from the remarks section of the DateTime.ToString Method (String) msdn page:
If format is null or an empty string, the general format specifier, 'G', is used.
The TryParse
overload you are using attempts to parse the DateTime
value using the date and time formats available in the IFormatProvider
format parameter - InvariantCulture
in your case - so when you use TryParse
with InvariantCulture
, unless your current culture's ShortDatePattern
and LongTimePattern
properties are the same as in InvariantCulture
, the tryParse
will fail.
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加