python获取当前时间和日期的方法

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

本文实例讲述了python获取当前日期和时间的方法。分享给大家供大家参考。具体如下:

>>> import datetime
>>> now = datetime.datetime.now()
>>> print now
2017-06-23 17:21:17.106729
>>> print "Year: %d" %now.year
Year: 2017
>>> print "Month: %d" % now.month
Month: 6
>>> print "Day: %d" % now.day
Day: 23
>>> print "Weekday: %d" %now.weekday()
Weekday: 4
>>> print "Hour %d" % now.hour
Hour 17
>>> print "Minute %d" %now.minute
Minute 21
>>> print "Second %d" % now.second
Second 17

>>> print "ISO weekday %d " % now.isoweekday()
ISO weekday 5 
>>> print "ISO Calendar: %s" %  str(now.isocalendar())
ISO Calendar: (2017, 25, 5)
>>> print now.strftime("%Y%m%d:%h%M:%S")
20170623:Jun21:17
>>> print now.strftime("%Y%m%d:%H%M:%S")
20170623:1721:17
>>> print now.strftime("%Y%m%d:%H:%M:%S")   
20170623:17:21:17

I  have  learning ...

转载于:https://my.oschina.net/xiaoxiezi/blog/1002602

你可能感兴趣的:(python)