Python Dates and Times in Python Let's Build a Timed Quiz App Simple Time Machine

time_machine.py

Please assist me with this challenge, is there an other way to solve it... do I have to go through if, elif, converting min, hours... into datetime format one by one, or is there more officiant way?

time_machine.py
import datetime

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

def delorean(int_time):
  new_time = datetime.datetime.fromtimestamp(int_time)
  return datetime.datetime.combine(starter, new_time)

2 Answers

Stephen Bone
Stephen Bone
12,359 Points

Hi Christine

Apologies if I'm covering something that hasn't been discussed yet as it's been a little while since I looked at this stage but assuming you've covered timedelta's you could do something like below:

import datetime

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

def delorean(num):
  return starter + datetime.timedelta(hours=num)

Hope it helps!

Stephen

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

You're not going to get a timestamp as the argument to delorean, you're going to get a number. Use that number to advance starter by that many hours. For example, if the number is 5, return a datetime that is starter plus 5 hours.