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 DateTime

Philipp Räse
PLUS
Philipp Räse
Courses Plus Student 10,073 Points

Region specific time format

I had an issue during the DateTime.TryParse Method.

I'm from Germany and my System has been set up in german. So my VisualStudio (which is relating to my system settings) tries to parse for a german time format.

After reading the DateTime.TryParse documentation I found out, that I should provide some further regional information for the DateTime-String I'd like to parse for.

Here's my code I'm currently using:

// values[0] → GameDate
DateTime gameDate;

CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");
DateTimeStyles styles = DateTimeStyles.None;

if (DateTime.TryParse(values[0], culture, styles, out gameDate))
{
    gameResult.GameDate = gameDate;
}

Maybe there's a prettier solution but at least it works properly.

I solved it by myself but I think it's pretty nice having this question here for anyone tackling the same issues.

1 Answer

Steven Parker
Steven Parker
229,771 Points

Good job resolving your issue! :+1: