Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Dates and Times in Python Dates and Times Wikipedia Links

Nataliya Fateeva
seal-mask
.a{fill-rule:evenodd;}techdegree
Nataliya Fateeva
Python Development Techdegree Student 4,530 Points

Why use datetime instead of string concatenation for Wiki Challenge?

It seems that "ask for date input/generate wiki link" challenge could be done easier by just concatenation of user input strings, since URL is basically a string (I am omitting all catch-error/loop parts):

month = input("Please type in month in EN (e.g. June) > ") month = month.capitalize() day = input("Please input day of the month as integer > ") final_date = f'{month}_{day}' wiki_format = f'https://en.wikipedia.org/wiki/{final_date}' print(wiki_format)

As we already accepted that we are not handling language-agnostic case (by hard-coding en link in the offered solution), it seems this is not a good case that actually needs strptime()/strftime() methods and using strings is simpler..

What am I missing?

2 Answers

AJ Tran
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
AJ Tran
Treehouse Teacher

Hi Nataliya Fateeva!

The beauty of programming is that there are many ways to do things! You make a valid point -- that this exercise can be completed with string concatenation. I know that the example is quite "contrived" -- it exists to introduce a new concept so that you can get some experience with using the datetime module.

In the "big picture," this is just one introduction to working with datetime and it will most certainly not be the last time that you will work with dates and times in your coding journey! There are also modules for calendar and time!!!

For example, when working with Databases, records are often saved with a timestamp that is hard to read for humans. It's a very common practice to convert those weird datetime into a "friendlier" string for humans to read.

You benefit now by recognizing that 1) Python has a built-in module for datetime and 2) you know specifically how to use methods such as strptime and strftime.

One BIG thing about these modules is that there is a lot of "under the hood" code that will seriously help you out, like with validating data. Hypothetically, what would happen in your program if a user gave you a non-real dates like "January 44" or "March -2"?

>>> import datetime

>>> datetime.date(2021, 1, -1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: day is out of range for month

How about keeping track of Leap Years, when February has one extra day? (2/29).

>>> datetime.date(2020, 2, 29)
datetime.date(2020, 2, 29)

>>> datetime.date(2021, 2, 29)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: day is out of range for month

These modules exist because they have all the code written to handle very common patterns that countless programmers have been dealing with since... the beginning of programming! Having a greater understanding of what is all part of the Python Standard Library will make you well-equipped :)

I hope this helps you!

Nataliya Fateeva
seal-mask
.a{fill-rule:evenodd;}techdegree
Nataliya Fateeva
Python Development Techdegree Student 4,530 Points

Thank you for the detailed response! I guess it was just me wishing for an exercise that would show case the usefulness of datetime's strptime/strftime methods in a more straight-forward way:)

cheers!:)