2011年1月11日 星期二

[Python] 時間格式轉換(strtime & strftime)

strftime 將 datetime object 轉換成String
strptime 將 datetime string 轉換成object

>>> datetime.now()
datetime.datetime(2011, 1, 11, 16, 19, 5, 211588)

>>> datetime.now().isoformat()
'2011-01-11T16:20:06.161541'

>>> datetime.strftime(datetime.now(), '%Y-%m-%d')
'2011-01-11'

>>> datetime.strptime(datetime.now().isoformat(), '%Y-%m-%dT%H:%M:%S.%f')
datetime.datetime(2011, 1, 11, 16, 24, 18, 411861)


See Also

http://docs.python.org/library/datetime.html#strftime-and-strptime-behavior
Related Posts Plugin for WordPress, Blogger...