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

Ashley Keeling
Ashley Keeling
11,476 Points

I am not sure why this isn't working

i don't know what i need to do, to make it work

timestrings.py
import datetime

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

def to_string(datetime):
    now = datetime.datetime.now()
    unites = input("")
    unites = unites.spilt("-")
    awnser = []

    if unites == "%d":
        awnser.apped(now.day)
    elif unites =="%B":
        awnser.append(now.month)
    elif unites == "%Y":
        awnser.append(now.year)

    awnser.join("")

1 Answer

I'll try to offer a little help just from a glance to point you in the right direction as to why nothing is working here:

  1. You're passing datetime as an argument to your to_string() function when you've already imported the datetime module. So in other words, if you're really intending to use datetime as an argument name in this function AND use the datetime module within this function, you might end up with some unexpected results.

  2. A few of your methods are misspelled, namely .spilt() and .apped() on lines 10 and 14 respectively.

  3. The formatting codes like %d, %B, etc. have no meaning outside of specific methods of datetime objects. Usually, these are used in methods like .strftime() and .strptime() but as standalone strings like you have in your conditions here, they have no meaning whatsoever.

  4. You don't return your awnser list at the end of the function so you can't get anything back out of the work this function does.

Judging from your comments though, I think what you're attempting to accomplish here can be as simple as the following:

import datetime

def to_string(dt_object):
    str_date = dt_object.strftime("%d %B %Y")
    return str_date

def from_string(str_date):
    dt_object = datetime.datetime.strptime(str_date, "%d %B %Y")
    return dt_object

now = datetime.datetime.now()

now_string = to_string(now)
# now_string is now "21 January 2018"

now_datetime = from_string(now_string)
# now_datetime is now a datetime.datetime(2018,1,21,0,0) object
Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

Moved comment to answer. Xander Morrison you've been added great answers as comments. I suggest you add them directly as answers instead! Thanks for your help in the community forum!

Ashley Keeling
Ashley Keeling
11,476 Points

thanks for the help but i dont fully get why you have called the argument 'dt_object' (the name of it)

thanks

You don't have to call it dt_object, that's just the parameter name I give to the function so that I know when I call that function, I need to pass to it a datetime object, in this case now since I define now = datetime.datetime.now(). In that function, I use .strftime() on the parameter you pass to the function. .strftime() is a method of a datetime object so what you pass to to_string() must be a datetime object as well. Otherwise, this will throw an error. So to summarize, you put a datetime object into the to_string() function, and you get the formatted string representation of that object out. And you put a string into the from_string() function, and you get a datetime object out of the function Hope this helps!

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

It is not a good idea to use built-in Python functions or imported modules names as variables names. In this case, using datetime as the literal variable name prevents accessing any other functions from the datetime module:

>>> def some_func(datetime):
...     # datetime is assumed to be a datetime object
...     # let's add some time using the timedelta function
...     datetime += datetime.timedelta(day=1)
... 
>>> some_func(datetime.datetime.now())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in some_func
AttributeError: 'datetime.datetime' object has no attribute 'timedelta'

This happens because "datetime" in the local namespace within the function is now set to the argument object passed in which has it's own methods but no knowledge of the other functions within the datetime module.