模块之time与datetime

模块之time与datetime

import time
print (time.clock())
print(time.process_time()) #测量处理器运算时间
print(time.altzone) #返回utc时间差,以秒计算
print(time.asctime())#返回时间格式
print(time.localtime())#返回本地时间的struct time 对象
print(time.gmtime(time.time()))#返回utc时间的struc时间,与中国本地时间相差8小时,中国是东八区。
print(time.asctime(time.localtime()))#返回时间格式
print(time.ctime())#返回时间格式,与上条相同




string_2_struct = time.strptime("2019/12/19","%Y/%m/%d")  #将日期字符品转换成struct时间格式
print(string_2_struct)
string_2_stamp= time.mktime(string_2_struct) #将struct时间对像转换成时间戳

print(time.gmtime(time.time()))#将UTC时间戳转换成struct_time格式
print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime() )) #将UTC struct_time 格式转成指定的字符串格式########2019-12-19 14:53:37


#时间的运算(时间加减)
import  datetime
print (datetime.datetime.now())  #返回当前时间
print(datetime.date.fromtimestamp(time.time()))#将时间戳直接转成日期格式
print (datetime.datetime.now()+datetime.timedelta(3))#当前时间加3天
print(datetime.datetime.now()+datetime.timedelta(-3))#当前时间减3天
print(datetime.datetime.now()+datetime.timedelta(hours=3))#当前时间加3小时
print(datetime.datetime.now()+datetime.timedelta(minutes=30))#当前时间加30分钟


c_time=datetime.datetime.now()
print(c_time.replace(minute=3,hour=2))#把时间替换成2点3分钟。



打印结果
3e-07
0.109375
-32400
Thu Dec 19 23:32:52 2019
time.struct_time(tm_year=2019, tm_mon=12, tm_mday=19, tm_hour=23, tm_min=32, tm_sec=52, tm_wday=3, tm_yday=353, tm_isdst=0)
time.struct_time(tm_year=2019, tm_mon=12, tm_mday=19, tm_hour=15, tm_min=32, tm_sec=52, tm_wday=3, tm_yday=353, tm_isdst=0)
Thu Dec 19 23:32:52 2019
Thu Dec 19 23:32:52 2019
time.struct_time(tm_year=2019, tm_mon=12, tm_mday=19, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=353, tm_isdst=-1)
time.struct_time(tm_year=2019, tm_mon=12, tm_mday=19, tm_hour=15, tm_min=32, tm_sec=52, tm_wday=3, tm_yday=353, tm_isdst=0)
2019-12-19 15:32:52
2019-12-19 23:32:52.735813
2019-12-19
2019-12-22 23:32:52.735813
2019-12-16 23:32:52.735813
2019-12-20 02:32:52.735813
2019-12-20 00:02:52.735813
2019-12-19 02:03:52.735813
posted @ 2019-12-19 23:33  科子  阅读(430)  评论(0编辑  收藏  举报