Python之time模块和datatime模块

import  time

time.sleep(5) #休眠
time.time() #返回系统时间戳 utc时间秒数
time.ctime() #返回字符串时间格式,也可以传入参数转换为字符串时间time.ctime(time.time())
time.gmtime() #返回时间结构体,包含年月日时分秒等成员,
 #如time.struct_time(tm_year=2017, tm_mon=6, tm_mday=12, tm_hour=7, tm_min=34, tm_sec=43...)
time.localtime() #返回本地时间,也可传入参数,类似ctime
time.mktime(time.localtime()) #传入时间对象,返回UTC时间戳
time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()) #传入时间对象,将struct_time转换成指定字符串格式
time.strptime("2017-06-12", "%Y-%m-%d") #将字符串转为时间对象

datetime模块

1 datetime.date.today() #返回当天,格式为:年-月-日
2 datetime.date.fromtimestamp() #传入UTC时间戳,转成日期格式
3 current_time = datetime.datetime.now()
4 datetime.datetime.now() #返回当前datetime对象,可以使用timetuple返回struct_time格式
5 current_time.timetuple()
6 current_time.replace(1995,6,16) #使用指定数据替代当前时间
7 datetime.datetime.strptime() #将字符串转换为日期格式
8 datetime.datetime.now() + datetime.timedelta(days = 10) #比现在时间加10天
9 datetime.datetime.now() + datetime.timedelta(hours = -10) #比现在时间减10个小时,其他数据类似
原文地址:https://www.cnblogs.com/xhcdream/p/7002598.html