text stringlengths 0 13.4k |
|---|
Console.WriteLine("Day: {0}; Month: {1}", date.Day, date.Month); |
Day: 10; Month: 11 |
Section 3.2: TryParse |
This method accepts a string as input, attempts to parse it into a DateTime, and returns a Boolean result indicating |
success or failure. If the call succeeds, the variable passed as the out parameter is populated with the parsed result. |
If the parse fails, the variable passed as the out parameter is set to the default value, DateTime.MinValue. |
TryParse(string, out DateTime) |
DateTime parsedValue; |
if (DateTime.TryParse("monkey", out parsedValue)) |
{ |
Console.WriteLine("Apparently, 'monkey' is a date/time value. Who knew?"); |
} |
This method attempts to parse the input string based on the system regional settings and known formats such as |
GoalKicker.com – .NET Framework Notes for Professionals |
10 |
ISO 8601 and other common formats. |
DateTime.TryParse("11/24/2015 14:28:42", out parsedValue); // true |
DateTime.TryParse("2015-11-24 14:28:42", out parsedValue); // true |
DateTime.TryParse("2015-11-24T14:28:42", out parsedValue); // true |
DateTime.TryParse("Sat, 24 Nov 2015 14:28:42", out parsedValue); // true |
Since this method does not accept culture info, it uses the system locale. This can lead to unexpected results. |
// System set to en-US culture |
bool result = DateTime.TryParse("24/11/2015", out parsedValue); |
Console.WriteLine(result); |
False |
// System set to en-GB culture |
bool result = DateTime.TryParse("11/24/2015", out parsedValue); |
Console.WriteLine(result); |
False |
// System set to en-GB culture |
bool result = DateTime.TryParse("10/11/2015", out parsedValue); |
Console.WriteLine(result); |
True |
Note that if you are in the US, you might be surprised that the parsed result is November 10, not October 11. |
TryParse(string, IFormatProvider, DateTimeStyles, out DateTime) |
if (DateTime.TryParse(" monkey ", new CultureInfo("en-GB"), |
DateTimeStyles.AllowLeadingWhite | DateTimeStyles.AllowTrailingWhite, out parsedValue) |
{ |
Console.WriteLine("Apparently, ' monkey ' is a date/time value. Who knew?"); |
} |
Unlike its sibling method, this overload allows a specific culture and style(s) to be specified. Passing null for the |
IFormatProvider parameter uses the system culture. |
Exceptions |
Note that it is possible for this method to throw an exception under certain conditions. These relate to the |
parameters introduced for this overload: IFormatProvider and DateTimeStyles. |
NotSupportedException: IFormatProvider specifies a neutral culture |
ArgumentException: DateTimeStyles is not a valid option, or contains incompatible flags such as |
AssumeLocal and AssumeUniversal. |
GoalKicker.com – .NET Framework Notes for Professionals |
11 |
Section 3.3: TryParseExact |
This method behaves as a combination of TryParse and ParseExact: It allows custom format(s) to be specified, and |
returns a Boolean result indicating success or failure rather than throwing an exception if the parse fails. |
TryParseExact(string, string, IFormatProvider, DateTimeStyles, out DateTime) |
This overload attempts to parse the input string against a specific format. The input string must match that format |
in order to be parsed. |
DateTime.TryParseExact("11242015", "MMddyyyy", null, DateTimeStyles.None, out parsedValue); // true |
TryParseExact(string, string[], IFormatProvider, DateTimeStyles, out DateTime) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.