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 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.