Python下time模块和datetime模块的使用
时间模块
time模块
时间表示格式:
1.格式化时间: 【 2018-01-01 01:00:00 】2.时间戳: 【 1518407077.940927 】 -> 秒数(从1970开始到现今)3.以元组方式表示,九个元素(格式化元组)元素如下:(1) tm_year=2018, # 年(2) tm_mon=2, # 月(3) tm_mday=12, # 日(4) tm_hour=11, # 小时(5) tm_min=49, # 分钟(6) tm_sec=13, # 秒(7) tm_wday=0, # 0(Mon)-6(Sun) 从0(周一)开始数 -> 6(周天)(8) tm_yday=43, # 今年的第几天(9) tm_isdst=0 # 时区,如果是1,则表示夏令时
使用
timezone
返回时间标准实际与当前时间差(秒数)In [45]: time.timezoneOut[45]: -28800In [46]: time.timezone / 3600Out[46]: -8.0 # 中国时间与标准时间相差8小时
altzone
与timezone
差不多daylight
查看你是否使用了夏令时,0表示没有使用In [50]: time.daylightOut[50]: 0
time ()
返回当前的时间戳In [37]: time.time()Out[37]: 1518407588.8324711
sleep ()
使程序暂停几秒In [51]: time.sleep(3)In [52]:
gmtime ()
传入时间戳,返回格式化元组 【 UTC时区 】In [54]: time.gmtime(time.time())Out[54]: time.struct_time(tm_year=2018, tm_mon=2, tm_mday=12, tm_hour=4, tm_min=7, tm_sec=50, tm_wday=0, tm_yday=43, tm_isdst=0)
localtime ()
传入时间戳,返回格式化元组(不传入参数,默认当前时间) 【 根据本地区域决定 】In [55]: time.localtime()Out[55]: time.struct_time(tm_year=2018, tm_mon=2, tm_mday=12, tm_hour=12, tm_min=10, tm_sec=13, tm_wday=0, tm_yday=43, tm_isdst=0)
In [56]: time.localtime(123123123)Out[56]: time.struct_time(tm_year=1973, tm_mon=11, tm_mday=26, tm_hour=8, tm_min=52, tm_sec=3, tm_wday=0, tm_yday=330, tm_isdst=0)
取出元组数据:
In [57]: a = time.localtime()In [58]: a.tm_year # 取出年份Out[58]: 2018In [59]: a.tm_mon # 取出月份Out[59]: 2In [60]: a.tm_sec # 取出分钟Out[60]: 54
mktime ()
由元组转换为时间戳In [63]: time.mktime(time.localtime())Out[63]: 1518409129.0
strftime (format,tuple) --> string
自定义生成格式化时间 -> string格式化操作
%Y Year with century as a decimal number.
%m Month as a decimal number [01,12].
%d Day of the month as a decimal number [01,31].
%H Hour (24-hour clock) as a decimal number [00,23].
%M Minute as a decimal number [00,59].
%S Second as a decimal number [00,61].
%z Time zone offset from UTC.
%a Locale's abbreviated weekday name.
%A Locale's full weekday name.
%b Locale's abbreviated month name.
%B Locale's full month name.
%c Locale's appropriate date and time representation.
%I Hour (12-hour clock) as a decimal number [01,12].
%p Locale's equivalent of either AM or PM
2. 使用:In [67]: time.strftime('%Y-%m-%d',time.localtime()) Out[67]: '2018-02-12'
ctime ()
传入时间戳,返回文本型时间In [69]: time.ctime() Out[69]: 'Mon Feb 12 12:30:44 2018'In [77]: time.ctime(123123123) Out[77]: 'Mon Nov 26 08:52:03 1973'
asctime ()
传入格式化元组,返回文本型时间In [79]: time.asctime(time.localtime(123123)) Out[79]: 'Fri Jan 2 18:12:03 1970'
In [80]: time.asctime() Out[80]: 'Mon Feb 12 12:34:23 2018'
datetime模块
datetie模块
datetime.now ()
返回当前时间In [156]: print(datetime.datetime.now())In [156]: 2018-02-12 12:39:43.309016
时间运算
In [174]: datetime.datetime.now() + datetime.timedelta(1)
Out[174]: datetime.datetime(2018, 2, 13, 12, 43, 12, 301435)
In [175]: datetime.datetime.now() - datetime.timedelta(1)
Out[175]: datetime.datetime(2018, 2, 11, 12, 43, 28, 154795)
In [181]: datetime.datetime.now() + datetime.timedelta(hours=1)
Out[181]: datetime.datetime(2018, 2, 12, 13, 44, 54, 816476)
In [182]: datetime.datetime.now() + datetime.timedelta(hours=1)
Out[182]: datetime.datetime(2018, 2, 12, 13, 44, 56, 429732)
转载于:https://blog.51cto.com/13155232/2071613
Python下time模块和datetime模块的使用相关推荐
- Python之路(第十六篇)xml模块、datetime模块
一.xml模块 xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单, xml比较早,早期许多软件都是用xml,至今很多传统公司如金融行业的很多系统的接口还主要 ...
- python中time模块的时间戳和格式化日期_Python中的time模块与datetime模块
time模块 time模块是包含各方面对时间操作的函数. 尽管这些常常有效但不是所有方法在任意平台中有效. time用struct_time表示时间 import time # time.struct ...
- python中time模块和datetime模块
python中time模块和datetime模块 ** time总结** 一.时间的表示 1.时间戳时间 是从伦敦的1970年1月1日 0点0分0秒开始的(与北京时间相差8个小时:1980.1.1.8 ...
- c linux time微秒_Python时间处理模块:time模块、datetime模块及日历模块Calendar
无论是那一个开发语言进行开发,时间的处理都是非常重要的,正确的处理时间是一个程序员开发中必备的技能,特别是对时间精度要求比较高,或者是要分清楚本地时间和服务器时间的这些业务要求,今天就来讲一讲Pyth ...
- Python_Task08:模块与datetime模块
模块与datetime模块 学习教程 模块 1. 什么是模块 2. 命名空间 3. 导入模块 4. if __name__ == '__main__' 5. 搜索路径 6. 包(package) da ...
- 二十六、深入Python中的time和datetime模块
@Author: Runsen time和datetime是Python处理日期和时间的标准库.在我们项目的实际开发当中使用率是非常高的.datetime模块有四个重要的类,date,time,dat ...
- python中time模块中的倒计时_Python中time模块与datetime模块在使用中的不同之处 python 的time模块获取的是什么时间...
python的datetime模块的一些问题 time_1 = datetime.datetime.now(pytz.timezone('Asia/Shanghai')) timeimport dat ...
- python time模块_Python time模块和datetime模块
一.time 模块 time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.struct_time时间元组, ...
- python自动化测试-D6-学习笔记之一(常用模块补充datetime模块)
# datetime 模块import datetime print(datetime.datetime.today()) #当前日期,到秒 打印结果:2018-01-21 10:23:46.0344 ...
- 【python】 time模块和datetime模块详解 【转】
一.time模块 time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.struct_time时间元组,共 ...
最新文章
- 潘建伟团队再次展示量子计算优越性!“祖冲之号”1.2小时就能完成超算8年计算量...
- 6.编译器拓展SEH
- poj 1151(线段树求面积并)
- 4412 字符类设备的设备号
- 致所有.Net者和有梦想的朋友们 - 共勉
- python之父的名字-Python 常见双下划线关键字的用法
- 计算机网络学习(九)—应用层的概述
- 【网络安全工程师面试合集】—不要随便浏览一些奇怪的小网站哦
- Emacs进阶之使用Google翻译单词及句子
- lambda表达式传参
- python模块学习---glob 文件路径查找
- 2018年华北五省计算机应用大赛参赛作品--战拖儿app
- 韦根协议——STM32收发
- android xposed 简书,Xposed 入坑篇
- 电脑重装系统Win10“initpki.dll”加载失败怎么办?
- 实现微信向多好友定时发送信息
- PCB走线和过孔通流能力的标准、影响因素及其计算软件
- C/C++实现PCA降维以及故障监测
- Aptina公司在台湾成立工程中心
- 划重点!选择收银软件必须注意的四大要点
热门文章
- java 异常抛出_Java 如何抛出异常、自定义异常、手动或主动抛出异常
- NLP之:百度SKEP
- 为什么说bagging是减少方差,而boosting是减少偏差
- 常见排序算法整理2(C++实现)
- prthon日期型、字符串、数值、时间戳相互转换
- 找工作知识储备(2)---数组字符串那些经典算法:最大子序列和,最长递增子序列,最长公共子串,最长公共子序列,字符串编辑距离,最长不重复子串,最长回文子串
- NP、P、NPC、NP-hard 概念辨析
- Activation function in Neural Network
- 如何使用Scylla进行OSINT信息收集
- Raki的NLP竞赛topline解读:NBME - Score Clinical Patient Notes