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 Time Tango

Is this Right?

I tried combining a date and time, as I was asked by the prompt, but the challenge wouldn't accept my code.

However, when I run it in the console, assigning date to one variable and time to another, combine them and use datetime.datetime.strptime to format it to a datetime object, it succeeds.

What am I doing wrong?

combo.py
import datetime

current_date = '2015-07-08 '
current_time = '12:19'

def time_tango(current_date, current_time):
    current_date_time = current_date + current_time
    return datetime.datetime.strptime(current_date_time, '%Y-%m-%d %H:%M')

2 Answers

Russell Sawyer
seal-mask
.a{fill-rule:evenodd;}techdegree
Russell Sawyer
Front End Web Development Techdegree Student 15,705 Points

The two parameters date and time are not strings, they are date and time objects. The package datetime actually has a method called combine which is used when we want to combine a date object with a time object (into a datetime object). Return the two objects combined. Here is the link to the documentation.

https://docs.python.org/2/library/datetime.html#datetime.datetime.combine

import datetime
def time_tango(date, time):
    return datetime.datetime.combine(date, time)

Right! Thank you Russell! I forgot that Kenneth had shown us the combine method at work.