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 Far Away

sidni ahmed
sidni ahmed
3,329 Points

far_away function

how would i use this function? what do i put in as the argument?

far_away.py
import datetime

def far_away(timedelt):

  return timedelt + datetime.datetime.now()

1 Answer

Hi Sidni,

I ran the following in the python shell a few minutes ago to show you an example of how you would use it.

>>> datetime.datetime.now()
datetime.datetime(2016, 1, 9, 20, 14, 1, 960597)
>>> far_away(datetime.timedelta(hours=5))
datetime.datetime(2016, 1, 10, 1, 14, 7, 424598)

First I got the current datetime. From that you can see that it's Jan. 9th, 2016 at approximately 8:14pm. The hour is in 24 hour format.

Next I called the far_away function. It takes a timedelta object. So I created a timedelta object that is exactly 5 hours long. It could be whatever you needed though. The function then returns the datetime that is 5 hours into the future.

We can see that would be Jan. 10, 2016 at approximately 1:14am.

You don't need anything more than what you have for the challenge though.

sidni ahmed
sidni ahmed
3,329 Points

Thank you. Very good explanation