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 strftime & strptime

Ava Jones
Ava Jones
2,597 Points

I do not understand the instructions

please help me!

## 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(date):
    return date.strftime("%d %B %Y")
def from_string("2014 12 2", strftime):
    return "2014 12 2" + strftime

2 Answers

Asher Orr
.a{fill-rule:evenodd;}techdegree seal-36
Asher Orr
Python Development Techdegree Graduate 9,086 Points

Hi Ava! You completed the first challenge successfully.

import datetime

def to_string(datetime):
    return datetime.strftime("%d %B %Y")

Your code takes a datetime object as an argument, and it returns a string. Perfect!

The second challenge asks for a function that takes 2 arguments:

  • a date as a string
  • an strftime-compatible format string

.. and returns a datetime created from them.

In your code, you passed in an actual string as the first argument:

def from_string("2014 12 2", strftime):

Remember that arguments are kind of like placeholders. You're saying to Python:

"I'm creating this function now. When I call it later, I'm going to pass in arguments.

I'm not sure what they'll be yet, but the first argument will be a date as a string. The second argument will be an strftime-compatible format string."

Therefore, it should look like this:

def from_string(date_as_string, strftime_formatted_string)

When you click "Check Work", the Treehouse code challenge is going to call your function. It will pass in a date as a string. Then it will pass in a string that works as an strftime-compatible format.

The challenge might run your code like this:

from_string("09/24/12 18:30", "%m/%d/%y %H:%M")

Now, you need to return a datetime object created from your arguments- both of which are strings.

This means you need to use the strptime method. Kenneth covers this in the "Dating Methods" video: https://teamtreehouse.com/library/dates-and-times-in-python/dating-methods#transcript

Let me know if you still don't understand. I hope this helps!

Steven Parker
Steven Parker
218,596 Points

The second challenge is similar to the first in that you will need to call a function from the datetime library, using the arguments that came in to your function, and return the result. You still need to identify the correct function and use it.

But you also have a syntax issue where you have used a string literal ("2014 12 2") as a parameter name. Parameter names must be chosen following the same rules as variable names.