Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Streams and Data Processing Parsing Data Working with Enums

Parsing US-format dates and times?

I found that the DateTime.TryParse(values[0], out gameDate) line was (silently) failing to parse the date. I suspected this might have been caused by my locale being set to UK, where we don't use the M/D/Y date format.

I managed to solve this with the following code:

DateTime gameDate;
CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");
DateTimeStyles styles = DateTimeStyles.None;
if (DateTime.TryParse(values[0], culture, styles, out gameDate))
{
    gameResult.GameDate = gameDate;
};

Is this expected behaviour for people outside the US? Could it be caused by something other than my current Windows/OS locale?

I was having the same issue - Thanks for posting Tom.

Damir Paulić
Damir Paulić
6,591 Points

Had a same problem, thanks!

2 Answers

James Churchill
STAFF
James Churchill
Treehouse Teacher

Tom,

Yes, that's the expected behavior, as .NET by default will assume that the string you're passing to the DateTime.TryParse method is in a format that your current culture expects. The solution that you came up with is the correct workaround.

Thanks ~James

Thank you Tom Dalton had the same issue. Can anyone explain how this code work?