python time datetime模块使用方法记录
2017-03-04 17:37
696 查看
import time import datetime #time datetime使用方法备份 print(time.clock()) # 4.665275792443746e-07 返回处理器时间 print(time.process_time()) # 0.0780005 print(time.time()) # 1488616321.8451998 返回当前时间戳 <class 'float'> print(time.ctime()) # Sat Mar 4 16:34:51 2017 格式化返回系统时间 <class 'str'> print(time.ctime(887665)) # Sun Jan 11 14:34:25 1970 print(time.ctime(time.time() - 60*60*24 )) # Fri Mar 3 16:39:17 2017 输出前一天 print(time.gmtime()) # time.struct_time(tm_year=2017, tm_mon=3, tm_mday=4, tm_hour=8, tm_min=41, tm_sec=32, tm_wday=5, tm_yday=63, tm_isdst=0) # <class 'time.struct_time'> 格林尼治时间 print(time.localtime()) # time.struct_time(tm_year=2017, tm_mon=3, tm_mday=4, tm_hour=16, tm_min=43, tm_sec=43, tm_wday=5, tm_yday=63, tm_isdst=0) # <class 'time.struct_time'> 当地时间 # !!此时时间格式为struct_time print(time.mktime(time.localtime())) # 1488617178.0 # 将struct_time时间转化为float格式的时间 print(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time() - 60*60*24))) # 2017-03-03 16:59:15 # 将struct_time转化为字符串类型的时间 # 首先:time.time() 为float格式 time.localtime() 的输入格式即为float,输出格式为struct_time # time.strftime 又将struct_time 转化为字符串输出格式 print(time.strptime('2017-03-03 16:59:15',"%Y-%m-%d %H:%M:%S")) # time.struct_time(tm_year=2017, tm_mon=3, tm_mday=3, tm_hour=16, tm_min=59, tm_sec=15, tm_wday=4, tm_yday=62, tm_isdst=-1) # 与strftime相反,输入为字符串格式,按照格式对应的字段转化为struct_time格式 # '2017-03-03 16:59:15'与"%Y-%m-%d %H:%M:%S"是一一对应的。 ---------- # datetime print(datetime.date.today(),type(datetime.date.today())) # 2017-03-04 <class 'datetime.date'> # 当前日期 print(datetime.date.fromtimestamp(time.time() - 60*60*24)) # 2017-03-03 # 将float格式的时间戳转化为<class 'datetime.date'>格式 current_time = datetime.datetime.now() print(current_time) # 2017-03-04 17:20:41.851214 当前时间 print(current_time.timetuple()) # time.struct_time(tm_year=2017, tm_mon=3, tm_mday=4, tm_hour=17, tm_min=21, tm_sec=42, tm_wday=5, tm_yday=63, tm_isdst=-1) # 将<class 'datetime.date'> 转化为 <class 'time.struct_time'> print(current_time.replace(2012)) # 2012-03-04 17:29:53.446764 # 替换时间并输出 newtime = datetime.datetime.now() - datetime.timedelta(days=3) print(datetime.datetime.now(),'\t',newtime) # 2017-03-04 17:32:47.736732 2017-03-01 17:32:47.736732 # days=3 为3天时间 -号则表示3天前
相关文章推荐
- Python时间模块datetime、time、calendar的使用方法
- Python中time,datetime模块的常见使用方法
- Python时间模块datetime、time、calendar的使用方法
- Python中time模块与datetime模块在使用中的不同之处
- Python中time模块与datetime模块在使用中的不同之处
- Python time、datetime、os、random、sys、hashlib、json、shutil、logging、paramiko、subprocess、ConfigParser、xml、shelve模块的使用
- python中datetime模块中datetime对象的使用方法
- python 中datetime、time、calendar模块中常用的方法
- Python中time模块与datetime模块在使用中的不同之处
- Python下time模块和datetime模块的使用
- Python使用 time和datetime模块随意操作 时间
- python中time,datetime模块常用方法总结
- 在python中使用cx_Oracle模块出现“DLL Load failed”的处理方法
- Python 模块paramiko简单使用方法
- Python optionParser模块的使用方法
- Python基本使用方法记录
- python simplejson模块的使用方法
- 在python中使用cx_Oracle模块出现“DLL Load failed”的处理方法
- Python 模块paramiko简单使用方法
- python中使用time模块计算代码执行效率的精度测试(转)