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

Daesik Kim
Daesik Kim
5,422 Points

What is the 'right datetime'? Please tell me what is wrong with this code.

def time_tango(date, time): now = datetime.datetime.now() date = now.strftime('%Y-%m-%d') time = now.strftime('%H:%M:%S:%f') dt_str = date + " " + time dt = datetime.datetime.strptime(dt_str, '%Y-%m-%d %H:%M:%S:%f') return dt

How should I fix this?

combo.py
import datetime


def time_tango(date, time):
  now = datetime.datetime.now()
  date = now.strftime('%Y-%m-%d')
  time = now.strftime('%H:%M:%S:%f')
  dt_str = date + " " + time
  dt = datetime.datetime.strptime(dt_str, '%Y-%m-%d %H:%M:%S:%f')
  return dt

3 Answers

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello Daesik.

What I meant is that you need to get rid of that now variable and everything related to that.

The main idea you have is good I think as you first wanted to get a date string and then a time string and in the end putting them together in the right form as requested by the code challenge.

Let see the case of date: You produced this piece of code:

date = now.strftime('%Y-%m-%d')

So, what I suggest is just get rid of this now variable that you have created as I do think we don't need it. We can still use the date variable, but we need to assign it based on the date value that the function takes, the code would then be:

date = date.strftime('%Y-%m-%d')

This will provide us with a string based on the date.

Same thing will apply to the time variable you want to create.

The rest of the code, where you put together the two instances, looks fine.

Please note that this is probably NOT the most elegant way to handle this as Ken suggested, but I thought your code was close enough to correct it a little and have it pass the challenge, as it is really close.

Vittorio

Daesik Kim
Daesik Kim
5,422 Points

Thank you very much! Finally solved!!!!

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Daesik;

I would take a look in the Python documentation for the implementation of the combine method for datetime objects. That might get you headed in the correct direction.

Post back if you are still stuck.

Happy coding,

Ken

Daesik Kim
Daesik Kim
5,422 Points

I am not sure what kind of output I should get. This is what I did based on your comment.

import datetime

def time_tango(date, time): d = datetime.date(2015, 4, 1) t = datetime.time(18, 50) dt = datetime.datetime.combine(d, t) return dt

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello Daesik.

You are actually pretty close, I only see a concept mistake, but your code is close to the solution.

Here is the problem.

You are setting a variable called "now" which stores the time at moment "now" which is not needed at all. We only need to consider date and time in this challenge as they function will take those as parameters. You only need to get rid of the now variable code line and then set date and time like you did with the strftime method BUT based on the date and time taken by the function and NOT on the now variable.

I hope I made it clear.

Vittorio

Daesik Kim
Daesik Kim
5,422 Points

Is this what you meant?

import datetime

def time_tango(date, time): d = datetime.date(date) t = datetime.time(time) dt = datetime.datetime.combine(d, t) return dt