I want to retrieve date in the format of "May 01 2009" from datetime.date object. I have a table stored in MySQL database. It has Date column, and Time Column separately. Date in table is of the format,
2009-05-01
I have connected to MySQL server using PyMySQL module,
conn = pymysql.connect("localhost", "root", "cloudera", "streaming")
cursor = conn.cursor()
sql = "select * from table1 limit 5;"
cursor.execute(sql)
row = cursor.fetchone()
row[0]
Output is,
datetime.date(2009, 5, 1)
When I try the below command to extract the date in the way I want,
datetime.date.strftime("%b %d %Y", row[0])
I get an error as,
TypeError: descriptor 'strftime' requires a 'datetime.date' object but received a 'str'
I just don't get it, when datetime.date object is provided, it raises an error. Can anyone please help me. Thanks in advance.
strftime
is a instance method of date
objects, not a separate function:
print(row[0].strftime("%b %d %Y"))
The error message is trying to tell you that you're calling the uninstanced method (descriptor) from the date
class directly, without passing an instance as first parameter.
For more info check date objects documentation.