Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime.TryParseExact and different separators

Tags:

.net

Is it possible to instruct DateTime.TryParseExact to accept multiple (any) sepatators (specify separator placeholder instead of particular separator)? For instance:

DateTime.TryParseExact(performanceRow[5], "M/d/yyyy", 
    CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces, out startDate)

This works if date has '/' character as separator. However I've just received a file with '.' as separator, and parsing fails.

Thanks

like image 493
dragonfly Avatar asked Dec 10 '12 07:12

dragonfly


1 Answers

Use this variant of TryParseExact

DateTime.TryParseExact(String, String[], IFormatProvider, DateTimeStyles, DateTime%)

MSDN link

The second param is a string array which is defined as

formats

Type: System.String[]

An array of allowable formats of s.

The description says: The format of the string representation must match at least one of the specified formats exactly.

like image 134
Ravi Y Avatar answered Sep 19 '22 01:09

Ravi Y