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点左右,我们在做线上数据库的备库时,误将线上 ...
随机推荐
- springboot默认Thymeleaf模板引擎js的解决方案
<script th:inline="javascript"> var btnexam=[[${btnexam}]]; console.log(btnexam); va ...
- 基于Ubuntu 18.04.5 LTS 部署Ceph集群测试及Ceph RDB的使用。
1.ceph简介 Ceph在一个统一的系统中独特地提供对象.块和文件存储 1.1 ceph官网架构图 1.2 架构解释 CEPH 对象存储 CEPH 块设备 CEPH 文件系统 RESTful 接 ...
- 一类巧妙利用利用失配树的序列DP
I.导入 求长度为\(\text{len}\)的包含给定连续子串\(\text{T}\)的 0/1 串的个数.(\(|T|<=15\)) 通常来说这种题目应该立刻联想到状压 DP 与取反集--这 ...
- [源码解析] PyTorch 分布式 Autograd (4) ---- 如何切入引擎
[源码解析] PyTorch 分布式 Autograd (4) ---- 如何切入引擎 目录 [源码解析] PyTorch 分布式 Autograd (4) ---- 如何切入引擎 0x00 摘要 0 ...
- 【R】ggplot2的facet_warp/grid如何实现超过两类水平的分面?
之前最多只做过两类单水平的分面,即两两组合的面板图.如果某类超过两个水平呢? 一类的分面:facet_wrap(~ align) 两类的分面(x轴和y轴):facet_grid(align ~ gen ...
- [linux] mv: cannot move $ to $: Directory not empty
最近测试某流程时,跑的过程报错了,于是检查脚本修改后重新测试.脚本是改过来了,但在shell中运行某步时碰到了如题报错! $ mv MP_genus_network_files/ tax_networ ...
- vs2019 16.8更新之后的 C++20 协程co_yield用法
由于搜索出来的帖子,都是老版本的实验协程,很多老的代码已经失去参考性,并且很复杂,所以就自己研究了一下. 1 #include <iostream> 2 #include <coro ...
- UE4 C++工程以Game模式启动
UE4版本:4.24.3源码编译版本 Windows10 + VS2019环境 UE4 C++工程,默认情况下VS中直接运行是启动Editor模式: 有时为了调试等目的需要以Game模式启动,可以避免 ...
- 计算机网络-4-7-内部网关协议OSPF
内部网关协议OSPF(开放最短路径优先) 出现的原因:为了克服RIP协议的缺点在1989年开发出来,**开放 表明OSPF协议不受任何厂家的限制.最短路径优先是因为使用了最短路径算法SPF**. OS ...
- 格式化代码(Eclipse 格式化代码块快捷键:Ctrl+Shift+F)
1.格式化java代码 : ①Ctrl+Shift+F 但是我们会遇到按 Ctrl+Shift+F不起作用的时候? Ctrl+Shift+F 在搜狗拼音里是简繁替换.一旦安装搜狗拼音这个快 ...