Why is datetime.strptime not working in this simple example?

196,626

Solution 1

You should be using datetime.datetime.strptime. Note that very old versions of Python (2.4 and older) don't have datetime.datetime.strptime; use time.strptime in that case.

Solution 2

You are importing the module datetime, which doesn't have a strptime function.

That module does have a datetime object with that method though:

import datetime
dtDate = datetime.datetime.strptime(sDate, "%m/%d/%Y")

Alternatively you can import the datetime object from the module:

from datetime import datetime
dtDate = datetime.strptime(sDate, "%m/%d/%Y")

Note that the strptime method was added in python 2.5; if you are using an older version use the following code instead:

import datetime, time
dtDate = datetime.datetime(*time.strptime(sDate, "%m/%d/%Y")[:6])

Solution 3

Because datetime is the module. The class is datetime.datetime.

import datetime
dtDate = datetime.datetime.strptime(sDate,"%m/%d/%Y")

Solution 4

You should use strftime static method from datetime class from datetime module. Try:

import datetime
dtDate = datetime.datetime.strptime("07/27/2012", "%m/%d/%Y")
Share:
196,626
Reinstate Monica - Goodbye SE
Author by

Reinstate Monica - Goodbye SE

Update: an agreement with Monica Cellio Previous bio: Now I've had it. Stack Exchange is pretending this never happened: Stack Overflow is doing me ongoing harm; it's time to fix it!. They've made a complete B*****m of it. They've had multiple warnings but just stopped listening. At a certain point you've got to draw the line. Time to leave SE. Thanks to the community for a great 10 years. I've learned a lot and had fun. Goodbye. -- The original "I'm Spartacus" aka Wikis. (Listen to Monica Cellio herself in an interview, here).

Updated on July 08, 2022

Comments

  • Reinstate Monica - Goodbye SE
    Reinstate Monica - Goodbye SE almost 2 years

    I'm using strptime to convert a date string into a datetime. According to the linked page, formatting like this should work:

    >>> # Using datetime.strptime()
    >>> dt = datetime.strptime("21/11/06 16:30", "%d/%m/%y %H:%M")
    

    My code is:

    import datetime
    dtDate = datetime.strptime(sDate,"%m/%d/%Y")
    

    where sDate = "07/27/2012". (I understand, from the same page, that %Y is "Year with century as a decimal number.")

    I have tried putting the actual value of sDate into the code:

    dtDate = datetime.strptime("07/27/2012","%m/%d/%Y")
    

    but this does not work. The error I get is:

    AttributeError: 'module' object has no attribute 'strptime'

    What am I doing wrong?

  • Reinstate Monica - Goodbye SE
    Reinstate Monica - Goodbye SE over 11 years
    Thanks, but then I get: AttributeError: type object 'datetime.datetime' has no attribute 'strptime'
  • Martijn Pieters
    Martijn Pieters over 11 years
    @Wikis: do you have another module named datetime in your project?
  • Reinstate Monica - Goodbye SE
    Reinstate Monica - Goodbye SE over 11 years
    No. datetime is used in another place: now = datetime.datetime.now() if that's any help.
  • Martijn Pieters
    Martijn Pieters over 11 years
    @Wikis: are you using a python version before 2.5, by any chance?
  • Anand
    Anand over 8 years
    In relation to the same question I am trying to do something like this - dt = datetime.datetime.strptime("2016-01-12T03:16:10.815675+00:00‌​", utc) <br> Ofcourse it is not working but just wondering is there a way to do it. I get a utc string and want to convert it to datetime object. Is there a simple way to do it?