python(5)–datetime模块

1. datatime.date.today()     当前日期

>>> import datetime
>>> datetime.date.today()
datetime.date(2016, 3, 13)

 

2. datetime.datetime.now()    当前时间

>>> datetime.datetime.now()
datetime.datetime(2016, 3, 13, 21, 21, 32, 523934)

 

3. datetime.datetime.today()    当前时间

>>> datetime.datetime.today()
datetime.datetime(2016, 3, 13, 21, 22, 0, 85805)

 

4.  datetime.date.fromtimestamp()     将时间戳日期输出

>>> datetime.date.fromtimestamp(1457877369.650549)
datetime.date(2016, 3, 13)

 

5. datetime.datetime.fromtimestamp()     将时间戳输出

>>> datetime.datetime.fromtimestamp(1457877369.650549)
datetime.datetime(2016, 3, 13, 21, 56, 9, 650549)

 

6. timetuple()  转换为strucktime

>>> datetime.datetime.now().timetuple()
time.struct_time(tm_year=2016, tm_mon=3, tm_mday=13, tm_hour=22, tm_min=1, tm_se
c=25, tm_wday=6, tm_yday=73, tm_isdst=-1)

 

7.  replace()       替换日期,传什么替换什么

>>> datetime.datetime.now().replace(2014, 9, 12)
datetime.datetime(2014, 9, 12, 22, 3, 9, 229790)

 

8. strptime()

>>> datetime.datetime.strptime("21/11/06 16:30", "%d/%m/%y %H:%M")
datetime.datetime(2006, 11, 21, 16, 30)

 

9. 日期时间的加减

new_time = datetime.datetime.now() + datetime.timedelta(days=10)  #当前时间+10天
new_time = datetime.datetime.now() + datetime.timedelta(days=-10)
new_time = datetime.datetime.now() - datetime.timedelta(days=10)    #这两个都是当前时间-10天
new_time = datetime.datetime.now() - datetime.timedelta(hours=10)
new_time = datetime.datetime.now() - datetime.timedelta(seconds=10)
posted @ 2016-03-13 22:10  黄小墨  阅读(286)  评论(0编辑  收藏  举报