开发者

How do I get my Python date in the format that I want?

开发者 https://www.devze.com 2023-01-05 20:58 出处:网络
I\'m reading a dat开发者_如何学Goe from an Excel cell in Python (using .Value on the cell)... the result that I get is:

I'm reading a dat开发者_如何学Goe from an Excel cell in Python (using .Value on the cell)... the result that I get is:

07/06/10 00:00:00

I thought this was a string, and so went about trying to figure out how to convert this to the format I need ("yyyyMMdd", or "20100706" in this example). However, after some playing around I realized that it is not being pulled as a string... Running type() on it returns <type 'time'> .

I then assumed that it was a Python time object, and tried using strftime on it to convert it to a string... but that didn't work either. It doesn't recognize the strftime method on the value.

Any idea on how to parse this properly and get the format I want? What am I doing wrong? (And if this clearly contains a date as well as a time, why is Python automatically considering it a time object?)


You can convert it to a time object like this:

import time
time.strptime(str(thetime), '%m/%d/%y %H:%M:%S')

And then you should be able to manipulate it to your hearts content.

HTH


Have you tried str(time)? That should give it to you in a string and then you can play around with the formatting all you like.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号