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 Timedelta Minute

anhedonicblonde
anhedonicblonde
3,133 Points

minutes.py - confused about how to go about this one

hi there, I'm having a little issue with datetime and timedelta... I'm not really sure how to go about this challenge. I gave it a shot as you can see but my answer is wrong wrong wrong. Can someone spell this out for me? thanks so much!! :) a

minutes.py
import datetime

def minutes(datetime1, datetime2):
  minutes = datetime1 - datetime2
  return round(minutes)

1 Answer

Steven Parker
Steven Parker
229,783 Points

:point_right: Here's a few hints:

  • Remember the challenge said, "You'll need to subtract the first from the second."
  • when you subtract datetimes, you get a timedelta
  • timedeltas have a method you can call to get the duration in seconds
  • you can calculate minutes when you have seconds
anhedonicblonde
anhedonicblonde
3,133 Points

Thanks for the reply. I think I need to review the video again because I am still lost (except I saw where I was trying to do the subtraction backwards).

update: i get now thanks

import datetime

def minutes(dt1, dt2):
    change = dt2 - dt1
    seconds = change.total_seconds()
    minutes = seconds / 60
    return round(minutes)