import time #返回一个时间戳 #print(time.time()) #返回当前时间的时间戳 会随着时间的变化而变化 #time.sleep() 是让程序暂停几秒后,继续运行的方法 # s_time = (time.time()) # time.sleep(3) # print(f"程序开始到结束一共运行了{s_time - time.time()}") #程序开始时间 - 程序运行结束的时间 得到本程序运行时间 #time.localtime(“写入时间戳”) 可以得到当前时区对象的时间 运行结果为元组的形式呈现 print(time.localtime()) #打印结果:time.struct_time(tm_year=2021, tm_mon=7, tm_mday=11, tm_hour=19, tm_min=12, tm_sec=14, tm_wday=6, tm_yday=192, tm_isdst=0) print(time.gmtime()) #与localtime方法相似,但是将一个时间戳转换为UTC时区时间 print(time.mktime(time.localtime())) #mktime是将struct_time格式的重新转换成时间戳 #strftime用格式化字符串的方式输出时间 print(time.strftime("%Y-%m-%d %H:%M:%S %p")) # 年 月 日 时 分 秒 上下午 输出结果为:2021-07-11 19:27:49 PM #strptime 将字符串格式转换为元组类型显示的时间 str_time = time.strftime("%Y-%m-%d%H:%M:%S") print(time.strptime(str_time,"%Y-%m-%d%H:%M:%S")) # 这里是 需要转换的字符串,字符串中对应的时间格式
Python中time模块中的常用命令
最新推荐文章于 2024-05-16 12:26:37 发布