0

I know questions like these get asked all the time, but my specific problem doesn't seem to come up (at least I can't find it).

So my problem is like this. I have a MySQL database which has lots of data in it, with one column being full of dates. When I pull these dates, I automatically store them into a list which works great.

But, I also have to format the dates to calculate with. For instance, if I work on one of the dates I may need to extract just the month number. Having imported datetime, I would have imagined it was simple with strftime, but it wasn't. The problem is that they are stored in a string format (list is called last_shipped).

The dates come into the list according to this format:

((datetime.datetime(2012, 11, 30, 0, 0),),)

So when I try and use strftime I get the error

TypeError: descriptor 'strftime' requires a 'datetime.date' object but received a 'str'

My question is, how do I convert a list full of these to a list of workable datetime objects?

Thanks in advance

EDIT:

I am using MySQLdb. An example of the code I have tried that produces the error above is:

z = datetime.datetime.strftime(gr, '%m')

In this case z is the datetime string I mentioned above.

4

1 回答 1

1

这将帮助您:

time.strptime(string[, format])

根据格式解析表示时间的字符串。返回值是 gmtime() 或 localtime() 返回的 struct_time。

time.strftime(format[, t])

将表示由 gmtime() 或 localtime() 返回的时间的元组或 struct_time 转换为格式参数指定的字符串。

于 2013-11-13T06:23:55.470 回答