模块小结如下:另外有time模块小结
from datetime import date, time, datetime, timedelta
datetime.now() # 当前日期元组
datetime.today() # 当前日期元组
d1 = '2018-05-09 10:20:15'
d2 = '2018-06-02 08:32:10'
d1_tuple = datetime.strptime(d1,"%Y-%m-%d %H:%M:%S") # 把指定日期转成日期元组
d2_tuple = datetime.strptime(d2,"%Y-%m-%d %H:%M:%S") # 把指定日期转成日期元组
time_delta = d2_tuple - d1_tuple # 两个日期元组相减,得到timedelta对象,此对象包含days, secondes, microseconds属性
datetime.fromtimestamp(1525918343.0) # 把时间戳转换成日期元组
datetime.strftime(d1_tuple,'%Y-%m-%d %H:%M:%S') # 把日期元组转成格式化日期
d1_tuple + timedelta(days=10) # 某日期10天后的日期元组
(d1_tuple + timedelta(days=10)).strftime("%Y-%m-%d %H:%M:%S") # 把日期元组转成格式化日期
d1_tuple.timetuple() # 把日期元组转成时间元组
from datetime import date, time, datetime, timedelta
datetime.today() # 当前日期元组
>>>datetime.datetime(2018, 8, 31, 16, 9, 20, 835155)
datetime.today() + timedelta(days = -1) # 昨天日期元组
>>>datetime.datetime(2018, 8, 30, 16, 12, 35, 303939)
datetime.now()
>>>datetime.datetime(2018, 8, 31, 16, 12, 54, 172760)
a = datetime.now() # 当前日期元组
datetime.strftime(a,'%Y-%m-%d %H:%M:%S') # 把日期元组转成格式化日期
>>>'2018-08-31 16:13:55'
datetime.strptime('2018-05-10 10:12:23','%Y-%m-%d %H:%M:%S') # 把指定日期转成日期元组
>>>datetime.datetime(2018, 5, 10, 10, 12, 23)
datetime.strptime('2018-05-10 10:12:23','%Y-%m-%d %H:%M:%S').timetuple() # 取得指定日期的时间元组
>>>time.struct_time(tm_year=2018, tm_mon=5, tm_mday=10, tm_hour=10, tm_min=12, tm_sec=23, tm_wday=3, tm_yday=130, tm_isdst=-1)
import time
time.mktime(datetime.strptime('2018-05-10 10:12:23','%Y-%m-%d %H:%M:%S').timetuple()) # 取得指定日期的时间戳
>>>1525918343.0