Avatar of rye004
rye004
Flag for United States of America asked on

I need help with a pattern for DateTime.TryParseExact in C#

I am trying to use DateTime.TryParseExact in C# to catch the following patterns:
 
Wed Sep 27 06:29:44 CDT 2017
Wed Oct 4 06:27:44 CDT 2017
Sat Oct 14 06:33:15 CDT 2017
Wed Oct 18 06:34:06 CDT 2017
Wed Nov 8 06:30:46 CST 2017
Sun Nov 19 19:44:15 CST 2017
 
How would you write the pattern for the times above?  Please note, I don't care about retaining the timezone information.
 
Thank you for looking at my posting.
 
C#

Avatar of undefined
Last Comment
Kelvin McDaniel

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Ryan Chong

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Andrei Fomitchev

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
rye004

ASKER
Thank you for your help.
Kelvin McDaniel

Just in case someone needs to know how to do this with DateTime.TryParseExact, the key is to use a few formatted strings and the overload that lets you try more than one simultaneously:

string[] formats= 
{
   "ddd MMM d HH:mm:ss CDT yyyy", 
   "ddd MMM d HH:mm:ss CST yyyy"
};

var dateString = "Wed Oct 4 06:27:44 CDT 2017";

if (DateTime.TryParseExact(dateString, formats, new CultureInfo("en-US"), DateTimeStyles.None, out var dateValue))
{
  // do something with dateValue
}

Open in new window

Here is a working fiddle based on the OP: https://dotnetfiddle.net/k4bn8i
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck