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

I cannot understand this datetime code

I have a few questions regarding a datetime challenge. The challenge is: Create a new function named from_string that takes two arguments: a date as a string and an strftime-compatible format string, and returns a datetime created from them.

Here is the correct final code:

## Examples
# to_string(datetime_object) => "24 September 2012"
# from_string("09/24/12 18:30", "%m/%d/%y %H:%M") => datetime
import datetime

def to_string(arg):
    return arg.strftime('%d %B %Y')

def from_string(arg1, arg2):
    return datetime.datetime.strptime(arg1, arg2)

1) What is an 'strftime-compatible format string'? Strftime makes a datetime into a string, so for something to be 'strftime-compatible' I'd imagine it was a datetime (not a string). Or is this just saying it's in the format of a datetime that has been converted into a string? But if it's that last thing, I don't get why they they wouldnt just say 'date as a string' again.

2) What is the from_string function actually doing? The challenge suggests it is returning only one datetime, but these are two separate datetime strings and we are never combining them.

4) Why does strftime take a string? How can the system (is that the right word?) analyze that input to know how to format the strftime string?

3) I'm a little confused about what's going on within the 'datetime' library and the 'datetime' class and then several methods. Let me explain my understanding and please correct or add anything. The line ```return arg.strftime('%d %B %Y') works because "arg" is an instance of the class datetime, within the library datetime. It is calling the method strftime and inputting into that method '%d %B %Y' which tells the method in what format to return the datetime as a string. The line 'datetime.datetime.strptime(arg1, arg2)' is calling the datetime library, then the datetime class, then the strptime method, and inputting arg1 and arg2 which converts them both into datetimes(?).

Thanks for the help!

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

Good questions. Let's see if I can answer them for you.

1) What is an 'strftime-compatible format string'? A datetime object is a precise definition of a moment in time. Using the strftime method will allow you to generate a string based on the datetime values. To identify what is included in the resulting string, the "format string" contains directives of what to display. The acceptable format directives are found in this chart. "Calling strptime() with incomplete or ambiguous ISO 8601 directives will raise a ValueError."

So if you only wanted the month of the datetime as a three-character string the format string would be "%b". You are formatting the string results not "formatting a datetime". The datatime object remains unchanged by strftime. The result is just the date as a specific string.

2) What is the from_string function actually doing? This function return a string representing the datetime object by listing "Day of the month as a zero-padded decimal number", "Month as locale’s full name", "Year with century as a decimal number", each separated by a space.

4) Why does strftime take a string? The strftime method is bound to a datetime instance. The method parses the input string, looks for the % directives, then grabs the requested data from its datatime object to construct the string. As mentioned above, the directives must be from a specific supported list.

3) I'm a little confused about what's going on within the 'datetime' library and the 'datetime' class and then several methods. Let me explain my understanding and please correct or add anything. The line ```return arg.strftime('%d %B %Y') works because "arg" is an instance of the class datetime, within the library datetime. It is calling the method strftime and inputting into that method '%d %B %Y' which tells the method in what format to return the datetime as a string. Correct so far!!

The line 'datetime.datetime.strptime(arg1, arg2)' is calling the datetime library, then the datetime class, then the strptime method, and inputting arg1 and arg2 which converts them both into datetimes(?). Not precisely.

strptime is a class method meaning it does not operate on an existing datetime instance. Instead, it creates and returns a new datetime instance. The first argument is a string containing values to be used to create a datetime object. The second string is how to interpret that string of values. For example if the first string was "7/8/2020" is that "July 8, 2020" or "Aug 7, 2020"? It would depend if the format string was "%m/%d/%Y" or "%d/%m/%Y". Exactly one datetime object is returned.

Post back if you need more help. Good luck!!