python3 5月26日 time模块常用时间转换 &datetime()模块学习 random()
import time
获取当前时间:
指定字符串格式:time.strftime("%Y-%m-%d %H:%M:%S")
当前时间戳:time.time()
当前时间元组格式:time.localtime()默认为获取当前时间的格式,
可以计算任何一个时间戳的格式化结果。
字符串转时间格式
将时间字符串转按指定格式换为元组<class 'time.struct_time'>
time.strptime(“字符串”,format("%Y-%m-%dT%H:%M:%SZ"))
将时间格式转换为时间戳:
time.mktime(‘class 'time.struct_time’格式的元组) #将时间格式元组转换为时间戳 <class 'float'>
time.gmtime(时间戳)将时间戳转换为UTC时间元组
time.localtime(时间戳)将时间戳转换为本地时区的时间元组
time.ctime(‘时间戳’)将时间戳转换为字符串(本地时间) 如Sat May 26 21:11:24 2018
time.asctime(时间元组)将时间格式元组转换为 特定固定格式字符串 如Sat May 26 21:11:24 2018
time.timezone() 查看和UTC标准时间相差的秒数 -28800秒
time.daylight 查看是否使用了夏令时
转换代码示例:import time,datetime
'''
0、GMT 时间字符串转换为时间戳
'''
test_date_str = 'Mon, 07 Dec 2020 09:10:56 GMT'
test_struct_time = time.strptime(test_date_str,'%a, %d %b %Y %H:%M:%S GMT')
#time.struct_time(tm_year=2020, tm_mon=12, tm_mday=7, tm_hour=9, tm_min=10, tm_sec=56, tm_wday=0, tm_yday=342, tm_isdst=-1)
time_stamp = time.mktime(test_struct_time)
time_stamp
1607303456.0
'''
1、将字符串转换为时间戳
'''
str = '2019-10-29 17:54:26'
timestamp = time.mktime(time.strptime(str,'%Y-%m-%d %H:%M:%S'))
'''
2、datetime时间转换为时间戳
'''
datetime.datetime.now().timestamp()
'''
3、将时间戳数字转换为 日期时间字符串
'''
num_timestamp = time.time()
time_str = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(num_timestamp))
'''
4、将datetime时间转换为字符串
'''
datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
'''
5、把字符串转成datetime:
'''
str ='2019-10-29 17:54:26'
datetime.datetime.strptime(str, "%Y-%m-%d %H:%M:%S"))
'''
6、把字符串转成time:
'''
time.strptime(str,'%Y-%m-%d %H:%M:%S')
'''
7、获取当前UTC时间字符串
'''
#方法1、
utctime = time.gmtime()
utc_str = time.strftime("%Y-%m-%dT%H:%M:%SZ", utctime)
#方法2、
datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
格式参照
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
%a 本地(locale)简化星期名称 %A 本地完整星期名称 %b 本地简化月份名称 %B 本地完整月份名称 %c 本地相应的日期和时间表示 %d 一个月中的第几天(01 - 31) %H 一天中的第几个小时(24小时制,00 - 23) %I 第几个小时(12小时制,01 - 12) %j 一年中的第几天(001 - 366) %m 月份(01 - 12) %M 分钟数(00 - 59) %p 本地am或者pm的相应符 一 %S 秒(01 - 61) 二 %U 一年中的星期数。(00 - 53星期天是一个星期的开始。)第一个星期天之前的所有天数都放在第0周。 三 %w 一个星期中的第几天(0 - 6,0是星期天) 三 %W 和%U基本相同,不同的是%W以星期一为一个星期的开始。 %x 本地相应日期 %X 本地相应时间 %y 去掉世纪的年份(00 - 99) %Y 完整的年份 %Z 时区的名字(如果不存在为空字符) %% ‘%’字符 |
#时间加减import datetime# print(datetime.datetime.now()) #返回 2016-08-19 12:47:03.941925#print(datetime.date.fromtimestamp(time.time()) ) # 时间戳直接转成日期格式 2016-08-19# print(datetime.datetime.now() )# 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)) #时间替换random()
import random
#随机位数验证码
str_int = 'abcdefghigklmnopqrstuvwxyz1234567890'
check_code = ''
test_check = random.sample(str_int,4)
for i in test_check:
check_code+=i
print(check_code)
#随机整数
print(random.randint(0,99))
#随机偶数(从0开始,步长为2)
print(random.randrange(0,100,2))
#随机奇数数(从1开始,步长为2)
print(random.randrange(1,100,2))
#随机浮点数
print(random.random()) #0-1之间
print(random.uniform(0,99))#指定范围之间
print(random.choice('agc')) #随机字符
#洗牌,打乱顺序功能
list1 = [1,2,3,4,5,6,7,8,9,0]
random.shuffle(list1)
print(list1)
python3 5月26日 time模块常用时间转换 &datetime()模块学习 random()的更多相关文章
- 10月26日 奥威Power-BI基于微软示例库(MSOLAP)快速制作管理驾驶舱 腾讯课堂开课啦
本次课是基于olap数据源的案例实操课,以微软olap示例库Adventure Works为数据基础. AdventureWorks示例数据库为一家虚拟公司的数据,公司背景为大型跨国生产 ...
- 2016年12月26日 星期一 --出埃及记 Exodus 21:21
2016年12月26日 星期一 --出埃及记 Exodus 21:21 but he is not to be punished if the slave gets up after a day or ...
- 2016年11月26日 星期六 --出埃及记 Exodus 20:17
2016年11月26日 星期六 --出埃及记 Exodus 20:17 "You shall not covet your neighbor's house. You shall not c ...
- 2016年10月26日 星期三 --出埃及记 Exodus 19:10-11
2016年10月26日 星期三 --出埃及记 Exodus 19:10-11 And the LORD said to Moses, "Go to the people and consec ...
- 2016年6月26日 星期日 --出埃及记 Exodus 14:23
2016年6月26日 星期日 --出埃及记 Exodus 14:23 The Egyptians pursued them, and all Pharaoh's horses and chariots ...
- GIS大讲堂内所有讲座的索引(更新至2008年6月26日)(转)
转自:http://www.cnblogs.com/xiexiaokui/archive/2008/11/20/1337934.html GIS大讲堂内所有讲座的索引(更新至2008年6月26日) ...
- 2013年12月26日 星期四 doxygen入门--很好
body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...
- 我的Python成长之路---第一天---Python基础(1)---2015年12月26日(雾霾)
2015年12月26日是个特别的日子,我的Python成之路迈出第一步.见到了心目中的Python大神(Alex),也认识到了新的志向相投的伙伴,非常开心. 尽管之前看过一些Python的视频.书,算 ...
- 下厨房6月26日数据丢失事故总结 MYSQL主分区被rm 命令误删除
下厨房6月26日数据丢失事故总结 MYSQL主分区被rm 命令误删除 http://tech.xiachufang.com/?p=18 在6月26日凌晨12点左右,我们在做线上数据库的备库时,误将线上 ...
随机推荐
- [NOIP2017 提高组] 列队
考虑我们需要维护的是这样一个东西. 即可能变化的只有每一行前\(m - 1\)个,和最后一列. 我们考虑对每一行开一个权值线段树,记录原本序列的第\(x\)个是否被一出,且用一个\(vector\)记 ...
- Codeforces 306D - Polygon(随机化+乱搞)
Codeforces 题目传送门 & 洛谷题目传送门 中考终于结束了--简单写道题恢复下状态罢. 首先这一类题目肯定没法用一般的方法解决,因此考虑用一些奇淫的乱搞做法解决这道题,不难发现,如果 ...
- Perl语言入门10-13
----------第十章 其他控制结构---------------- unless结构 unless($fred =~ /\A[A-Z_\w*\z]/i){print "yes" ...
- Linux运维工程师面试题整理
1. Nginx 反向代理,负载均衡,动静分离,工作原理及优化nginx配置反向代理. vim Nginx.confServer模块中配置Listen 80Server_name ip;在server ...
- (转载)Java里新建数组及ArrayList java不允许泛型数组
java中新建数组: String[] s;//定义的时候不需要设置大小 s = new String[5];//为数组分配空间时就要设置大小 对于ArrayList, ArrayList< ...
- 1005.K次取反后最大化的数组和
1005.K次取反后最大化的数组和 目录 1005.K次取反后最大化的数组和 题目 题解 排序+维护最小值min 题目 给定一个整数数组 A,我们只能用以下方法修改该数组:我们选择某个索引 i 并将 ...
- 爬虫系列:存储 CSV 文件
上一期:爬虫系列:存储媒体文件,讲解了如果通过爬虫下载媒体文件,以及下载媒体文件相关代码讲解. 本期将讲解如果将数据保存到 CSV 文件. 逗号分隔值(Comma-Separated Values,C ...
- 从源码看RequestMappingHandlerMapping的注册与发现
1.问题的产生 日常开发中,大多数的API层中@Controller注解和@RequestMapping注解都会被使用在其中,但是为什么标注了@Controller和@RequestMapping注解 ...
- Linux启动初始化配置文件
Linux启动初始化配置文件(1)/etc/profile 登录时,会执行. 全局(公有)配置,不管是哪个用户,登录时都会读取该文件. (2)/ect/bashrc Ubuntu没有此文件,与之对应的 ...
- API接口设计之token、timestamp、sign 具体架构与实现(APP/小程序,传输安全)
Java生鲜电商平台-API接口设计之token.timestamp.sign 具体设计与实现 说明:在实际的业务中,难免会跟第三方系统进行数据的交互与传递,那么如何保证数据在传输过程中的安全呢(防窃 ...

