time 与 datetime 模块的常用方法
时间格式
一个名词:
UTC(Coordinated Universal Time):格林威治天文时,世界标准时间。中国为东八区 UTC+8
在编程的世界中,一定要了解的几种时间格式:
1、时间戳
从 1970-1-1 00:00:00 开始按秒计算的浮点型偏移量。
2、格式化的时间字符串
例如:2018-11-1 11:35:00
3、元组形式(struct_time)的时间,在python中包含9个元素
形式: time.struct_time(tm_year=2018, tm_mon=10, tm_mday=31, tm_hour=18, tm_min=30, tm_sec=39, tm_wday=2, tm_yday=304, tm_isdst=0)
含义:0--年,1--月,2--日,3--时,4--分,5--秒,6--周几(取值0~6,0表示周日),7--一年中的第几天,8--是否夏令时
time 模块
1、time.sleep(secs) 线程推迟指定时间运行,很常用。
2、time.time() 返回当前时间戳
>>>import time
>>>time.time()
1541043502.846
3、time.localtime([secs]) time.gmtime([secs]) 参数默认传入time.time(),将时间戳转换为 struct_time ,前者返回当地时间,后者返回标准时间。
>>>time.localtime()
time.struct_time(tm_year=2018, tm_mon=11, tm_mday=1, tm_hour=11, tm_min=47, tm_sec=49, tm_wday=3, tm_yday=305, tm_isdst=0)
>>>time.gmtime()
time.struct_time(tm_year=2018, tm_mon=11, tm_mday=1, tm_hour=3, tm_min=47, tm_sec=56, tm_wday=3, tm_yday=305, tm_isdst=0)
4、time.mktime(struct_time) 将 struct_time 转换为时间戳
>>>time.mktime(time.localtime())
1541044257.0
5、time.strftime(format[, struct_time]) 将 struct_time 转换为 格式化的时间字符串,struct_time 默认 time.localtime()
>>>time.strftime('%Y-%m-%d %X')
'2018-11-01 11:58:08'
6、time.strptime(string[, format]) 将 格式化的时间字符串转换为 struct_time
>>> time.strptime('2018-11-1 12:01', '%Y-%m-%d %H:%M')
time.struct_time(tm_year=2018, tm_mon=11, tm_mday=1, tm_hour=12, tm_min=1, tm_sec=0, tm_wday=3, tm_yday=305, tm_isdst=-1)
7、time.asctime([struct_time]) time.ctime([secs]) 前者将 struct_time , 后者将时间戳 转换为 这种格式:'Thu Nov 1 12:07:27 2018'
格式化的时间字符串有个对应关系表,如 %M 表示分钟 %m 表示月份 ,可自行查询。
datetime模块
datetime模块主要定义了以下几个类:
datetime.date 表示日期的类
datetime.time 表示时间的类
datetime.datetime 表示日期时间的类
datetime.timedelta 表示时间间隔
其中 datetime.timedelta(days) 接受一个参数,默认为 天,也可以指定 datetime.timedelta(hours=4),可直接跟datetime的其它类进行时间运算。
time 与 datetime 模块的常用方法的更多相关文章
- Python第十五天 datetime模块 time模块 thread模块 threading模块 Queue队列模块 multiprocessing模块 paramiko模块 fabric模块
Python第十五天 datetime模块 time模块 thread模块 threading模块 Queue队列模块 multiprocessing模块 paramiko模块 fab ...
- python的datetime模块处理时间
python的datetime模块主要用来处理时间,里面包含很多类,包括timedelay,date,time,datetime等 开发中经常会用到模块里面的datetime类,这是一个表示日期时间的 ...
- Python3学习之路~5.2 time & datetime模块
time模块 时间相关的操作,时间有三种表示方式: 时间戳 1970年1月1日之后的秒,即:time.time() 格式化的字符串 2014-11-11 11:11, ...
- collections、time和datetime模块
主要内容: 一.collections模块 二.time模块 三.datetime模块 1️⃣ collection模块 1.什么是collections模块.干什么用? collections模块 ...
- time&datetime模块
在Python中,和时间处理相关的模块有time,datatime,calendar(不常用)三个. UTCC(Coordinated Universal Time,世界协调时)亦即格林威治天文时间, ...
- python中time、datetime模块的使用
目录 python中time.datetime模块的使用 1.前言 2.time模块 1.时间格式转换图 2.常用方法 3.datetime模块 python中time.datetime模块的使用 1 ...
- python中datetime模块
Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块我在之前的文章已经有所介绍,它提供 的接口与C标准库time.h基本一致.相比于time模块 ...
- python datetime模块参数详解
Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块,它提供 的接口与C标准库time.h基本一致.相比于time模块,datetime模块的接 ...
- Python处理时间 time && datetime 模块
Python处理时间 time && datetime 模块 个人整理,获取时间方式: import datetime import time #获取当前时间:Thu Nov 03 ...
随机推荐
- NetCore持续踩坑
坑1: vs2017 安装 .netcore2.2.2后,新建项目编译报错:.NET SDK 不支持降.NET Core2.2 设置为目标. 我以为是.netcore的sdk版本有误,于是我查看.ne ...
- 让网站永久拥有HTTPS - 申请免费SSL证书并自动续期 Let’s Encrypt
让网站永久拥有HTTPS - 申请免费SSL证书并自动续期 Let’s Encrypt 为什么要用HTTPS 网站没有使用HTTPS的时候,浏览器一般会报不安全,而且在别人访问这个网站的时候,很有可 ...
- Exe资源文件
在 Win NT/2000/XP/2003 下,有现成的API函数,这些函数在 Win 9x/ME 下不能使用. HANDLE BeginUpdateResource(LPCTSTR pFileNam ...
- 0x11栈之Editor
参考链接:https://blog.csdn.net/SSLGZ_yyc/article/details/81700623 对顶栈的思想: 建立两个栈,栈A存储从序列开头到当前光标的位置的一段序列,栈 ...
- Ember.js 看法,精辟
https://ruby-china.org/topics/31451#reply43 都是大牛,或许还是vue适合小白!架不住人多啊!一个非常优秀的产品,客户百十号人,如何发展! 46楼的感想如下, ...
- jquery AJAX数据传输路径写法~
$.post('{:url("index/index/logininfo")}',{'username':name,'password':pwd},function(data){ ...
- Job for docker.service failed because the control process exited with error
Docker 无法启动 报错信息:Job for docker.service failed because the control process exited with error 找了很久才解决 ...
- Oracle 《积累章》 根据身份证号码更新当前出生日期
,),'yyyy-MM-dd') 函数使用 to_date() 日期转换函数 to_date(substr(t.sfzh,7,8),'yyyy-MM-dd') 将“19901212” 转 ...
- PHP算法练习1:两数之和
leetcode地址:https://leetcode-cn.com/problems/two-sum/description/ 题目: 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. ...
- Kafka集群安装部署、Kafka生产者、Kafka消费者
Storm上游数据源之Kakfa 目标: 理解Storm消费的数据来源.理解JMS规范.理解Kafka核心组件.掌握Kakfa生产者API.掌握Kafka消费者API.对流式计算的生态环境有深入的了解 ...