That's Another Way to Go About It

I generally don't like to showcase other's inelegant code, but this couldn't be more timely given yesterday's Twitter created_at parsing tip. There's always more than one way to do something, I guess.

       private static DateTime ParseDateString(string DateString)
{
Regex re = new Regex(@"(?<DayName>[^ ]+) (?<MonthName>[^ ]+) (?<Day>[^ ]{1,2}) ↵
(?<Hour>[0-9]{1,2}): (?<Minute>[0-9]{1,2}): (?<Second>[0-9]{1,2}) ↵
(?<TimeZone>[+-][0-9]{4}) (?<Year>[0-9]{4})");
Match CreatedAt = re.Match(DateString);
DateTime parsedDate = DateTime.Parse(
string.Format(
"{0} {1} {2} {3}:{4}:{5}",
CreatedAt.Groups["MonthName"].Value,
CreatedAt.Groups["Day"].Value,
CreatedAt.Groups["Year"].Value,
CreatedAt.Groups["Hour"].Value,
CreatedAt.Groups["Minute"].Value,
CreatedAt.Groups["Second"].Value));

return parsedDate;
}

For me, the lesson here is to know your libraries and always assume that someone else has done it better than you already. It then becomes a quest to find that better solution to the problem.

About this Entry

This page contains a single entry by bbrown published on September 12, 2008 3:46 PM.

How to Parse DateTimes from the Twitter API was the previous entry in this blog.

Not So Swift is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Feedback to