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 trialTom Dalton
1,793 PointsParsing 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?
Damir PauliΔ
6,591 PointsHad a same problem, thanks!
2 Answers
James Churchill
Treehouse TeacherTom,
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
Christian Fuchs
Full Stack JavaScript Techdegree Graduate 22,017 PointsThank you Tom Dalton had the same issue. Can anyone explain how this code work?
Thomas Fletcher
7,284 PointsThomas Fletcher
7,284 PointsI was having the same issue - Thanks for posting Tom.