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

challenge task 2 of 2

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 i don't where am loosing it it says from_string am getting the current date which is 02-02-2015 00:00:00 while expected is 21-10-2015 00:00:00

timestrings.py
import datetime 
def to_string(datetime_object):
  return datetime_object.strftime("%d %B %Y")

def from_string(arg1, arg2): 
  return datetime.datetime.combine(datetime.date.today(),datetime.time())

3 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

Well, in your from_string method, it takes 2 arguments, but you didn't use any of them in the method body, which you should.

def from_string(arg1, arg2): 
  return datetime.datetime.strptime(arg1, arg2)
Alexander Torres
Alexander Torres
4,486 Points

I hope this helps! :)

def from_string(arg1, arg2): arg1 = "09/24/12" arg2 = "%m/%d/%y" return datetime.datetime.strptime(arg1, arg2)

Jingru Zhang
Jingru Zhang
3,641 Points

Why strftime() doesn't need "datetime.datetime." before it but strptime() needs it?

Kyle Salisbury
seal-mask
.a{fill-rule:evenodd;}techdegree
Kyle Salisbury
Full Stack JavaScript Techdegree Student 16,363 Points

It's because in the strftime() function, the argument being placed is the datetime.datetime command. So whatever your argument name is, in the case of the guy above it is datetime_obeject, in reality it is just a code name for datetime.datetime.now()