home changes contents help options

197:日月をフォーマットする。

strftime を使います。

 >>> from datetime import date 
 >>> now = date.today() 
 >>> now 
 datetime.date(2003, 12, 2) 
 >>> now.strftime("%m-%d-%y or %d%b %Y is a %A on the %d day of %B") 
 12-02-03 or 02Dec 2003 is a Tuesday on the 02 day of December 
 >>> import datetime
>>> datetime.datetime.now()
datetime.datetime(2008, 10, 2, 21, 26, 23, 669939)
>>> datetime.datetime.now().strftime('%y-%m-%d: %H:%M:%S')
'08-10-02: 21:28:08'
>>> datetime.datetime.now().strftime('%Y-%m-%d: %H:%M:%S')
'2008-10-02: 21:28:22'