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 Let's Build a Timed Quiz App Simple Time Machine

Welser Muñoz
Welser Muñoz
4,447 Points

Simple Time Machine

Hi, I have a doubt!, I don't really understand what you are asking me the question.

import datetime

starter = datetime.datetime(2015, 10, 21, 16, 29)

def delorean(a):
  now = datetime.datetime.now()
  diff = starter - now
  seconds = diff.total_seconds()
  hours = round(seconds/3600)
  return hours

3 Answers

Dan Johnson
Dan Johnson
40,532 Points

This challenge is showing the use of timedelta objects. timedeltas let you treat date and times more like basic numeric types. You can add or subtract them to move forward or back in time. For example:

import datetime

starter = datetime.datetime(2015, 10, 21, 16, 29)
delta = datetime.timedelta(hours=1)

one_hour_ahead = starter + delta


Will print out:

2015-10-21 17:29:00

So to use this in the challenge you'll want to make a function that can take one argument that represents how many hours someone wants to move ahead from the starter date, and return that new date.

to simplify:

import datetime

starter = datetime.datetime(2015, 10, 21, 16, 29)

def delorean(num):
    return starter.replace(hour = starter.hour + num)