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

Python Dates and Times in Python (2014) Dates and Times Wikipedia Links

lindseyk
lindseyk
4,506 Points

Strptime formatting question

I'm curious why we need to format the date using strptime, if we are then going to turn around and reformat using strftime in the next step. Couldn't we just format the string given to us from the user input, so that it appears correctly in the Wikipedia link, without strp? In other words, why do we need to turn it into a datetime format and then back into a string? (I guess I'm missing a step in the logic.) Thanks!

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

Good question! Yes, it possible to write the code to directly take the user input and coerce into the correct wikipedia link format.

Using the two step approach provides some advantages:

  • Using strptime provides validation of both the format and checks for legal dates. This saves having to add extra code for date validation.
  • Users prefer to enter dates in an expected format. Trying to get them to enter "05_23" and include leading zeroes (and checking they do) can lead to frustrated users. A bit of UX consideration goes a long way!
  • Once you have a datetime object, you can easily convert to any other format or perform other date math calculations such as "and the next seven days of links" or "one year ago" link.
lindseyk
lindseyk
4,506 Points

Ah, that is super helpful! I see how it could take even longer to format directly from the string...that makes sense. (As well as more ideas for use!) Thanks so much for the explanation!