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 strftime & strptime

Courses Plus Student 2,434 Points

Date & Time

Create a new function named from_string that takes two arguments: a date as a string and an strftime-compatible format string, and returns a datetime created from them.

def from_string(date1, date2):  
  x1 = datetime.datetime.strptime(date1, "%m/%d/%y")
  x2 = datetime.datetime.strptime(date2, "%H:%M")  
  return datetime.datetime.combine(x1, x2)

can anyone confirm me my understanding on challenge ?. there is some mistake.

2 Answers

Ryan Merritt
Ryan Merritt
5,789 Points

The two arguments being passed into from_string() are not two datetime objects as your argument names (date1 and date2) suggest. Instead the arguments are a date represented as a string, and a format for how that string should be parse. datetime.datetime.strptime() is a great way to turn a string into a datetime object. It takes a date as a string and a format as its two arguments, which is exactly what you have. Good luck!