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

Clayton Perszyk
MOD
Clayton Perszyk
Treehouse Moderator 48,723 Points

datetime problems

This is my code for the time tango challenge and it seems like it should return a datetime object; but, it is not passing.

import datetime

def time_tango(date, time):
  formatStr = "{} {}".format(date, time)
  return datetime.datetime.strptime(formatStr, "%m/%d/%Y %I:%M:%S")
Martin Luckett
Martin Luckett
32,591 Points

Clayton

I have the same problem (my code below)

import datetime

def time_tango(date, time):
  datetimeFormat = '%m/%d/%y %h:%M'
  dateStr = "{0} {1}".format(date, time)
  result = datetime.datetime.strptime(dateStr, datetimeFormat)
  return result

Kenneth Love help please.

3 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

You have a time object and a date object. Neither one will just turn into the string that you're expecting on their own (that's why strftime exists), but there's an easier way that I covered in the video. A method named combine().

Martin Luckett
Martin Luckett
32,591 Points

Thanks Kenneth. That worked great.

Clayton Perszyk
Clayton Perszyk
Treehouse Moderator 48,723 Points

Thanks Kenneth for the link to the combine method; that definitely fixed my problem.

Hunter MacDermut
Hunter MacDermut
15,865 Points
import datetime

def time_tango(d, t):
  dt = datetime.datetime.combine(d, t)
  dt = dt.strftime("%m/%d/%y %H:%M")
  return datetime.datetime.strptime(dt, "%m/%d/%y %H:%M")

Still struggling with this one. Tried recreating this locally with what I assumed the variables were that you were passing in:

import datetime

today = datetime.datetime.today()
date = today.date()
time = today.time()

def time_tango(d, t):
  dt = datetime.datetime.combine(d, t)
  dt = dt.strftime("%m/%d/%y %H:%M")
  return datetime.datetime.strptime(dt, "%m/%d/%y %H:%M")

print(time_tango(date, time))

Returns:

2014-11-17 15:20:00

And I get what I think is the desired result. It would be helpful to clarify whether "date" and "time" are datetime objects or strings, and in what format the output should be. The challenge specifies the output should be a datetime, but you suggest using the combine method, which requires the "date" and "time" to be strings. Here's what I get if "date" and "time" are strings:

import datetime

date = "11/17/14"
time = "3:15:00"

def time_tango(d, t):
    return datetime.datetime.strptime(d + " " + t, "%m/%d/%y %H:%M:%S")

print(time_tango(date, time))

Returns:

2014-11-17 03:15:00

Neither approach passes the challenge, so I must be overlooking something obvious. A nudge in the right direction would be greatly appreciated.

Clayton Perszyk
Clayton Perszyk
Treehouse Moderator 48,723 Points

I had a similar problem. Forget about strptime and strftime for this challenge. The only method you need is datetime.datetime.combine(). Kenneth provided a link to the documentation above, if you need to look into the combine() method further.

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

I'm gonna make the prompt a bit more obvious for this one. It's obviously not obvious (hah) enough.

Hunter MacDermut
Hunter MacDermut
15,865 Points

Thanks Clayton for the tip and Kenneth for the clarification. Got it on the first try with the improved prompt!