当前位置:网站首页>time标准库
time标准库
2022-07-07 14:24:00 【努力卷】
传送门:零基础Python手把手学编程课程2020最新Python零基础入门课程(完结)想学Python这部视频就够了_哔哩哔哩_bilibili
time模块中三种时间表示方式
- 时间戳
- 结构化时间对象
- 格式化时间字符串
时间戳
import time
时间戳 1970.1.1 到指定时间的间隔 单位是秒
time.time() 生成当前时间的时间戳
time.time()-3600 一个小时之前的时间戳
结构化时间对象
st = time.localtime()
print(type(st))
print(st)输出结果:
<class 'time.struct_time'>
time.struct_time(tm_year=2022, tm_mon=6, tm_mday=17, tm_hour=16, tm_min=15, tm_sec=12, tm_wday=4, tm_yday=168, tm_isdst=0)
st 本质上是一个元组
print('今天是{}-{:02d}-{}'.format(st[0],st[1],st[2]))
print('今天是 星期{}'.format(st.tm_wday+1))输出结果:
今天是2022-06-17
今天是 星期5对象的属性是只读的 不能改
格式化时间字符串
print(time.ctime())输出结果:
Fri Jun 17 16:20:20 2022strftime(时间格式)'%Y-%m-%d %H:%M:%S'
print(time.strftime('%Y-%m-%d %H:%M:%S'))
print(time.strftime('%Y年%m月%d日 %H时%M分%S秒'))输出结果:
2022-06-17 16:25:44
2022年06月17日 16时25分44秒如果报错:
UnicodeEncodeError: 'locale' codec can't encode character '\u5e74' in position 2: encoding error
就需要在代码中添加:
import locale
locale.setlocale(locale.LC_CTYPE,'chinese')sleep:
t1 = time.time()
print('sleep begin...')
time.sleep(1.23)
print('sleep end')
t2 = time.time()
print("执行了{:.3f}秒".format(t2-t1))三种格式之间的转换:
时间戳 转换为 结构化对象
# UTC时间
time.gmtime(time.time())
time.gmtime()# local
print(time.localtime())
print(time.localtime(time.time()))结构化对象 转换为 时间戳
# mktime(st)
print(time.time())
print(time.mktime(time.localtime()))结构化对象 转换为 格式化时间字符串
# strftime(format,st)
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()))
print(time.strftime('%Y-%m-%d %H:%M:%S',time.gmtime()))格式化字符串 转换为 结构化时间对象
# strptime(str, format)
strtime = '2020-07-25 13:23:18'
print(time.strptime(strtime,'%Y-%m-%d %H:%M:%S'))
边栏推荐
- Laravel5.1 Routing - routing packets
- [flower carving experience] 15 try to build the Arduino development environment of beetle esp32 C3
- 全网“追杀”钟薛高
- C语言进阶——函数指针
- 95. (cesium chapter) cesium dynamic monomer-3d building (building)
- Three. JS series (2): API structure diagram-2
- Tidb cannot start after modifying the configuration file
- Asyncio concept and usage
- Build an all in one application development platform, light flow, and establish a code free industry benchmark
- Personal notes of graphics (2)
猜你喜欢
随机推荐
PHP实现执行定时任务的几种思路详解
水平垂直居中 方法 和兼容
Multiplication in pytorch: mul (), multiply (), matmul (), mm (), MV (), dot ()
面试题 01.02. 判定是否互为字符重排-辅助数组算法
Iptables only allows the specified IP address to access the specified port
PHP实现微信小程序人脸识别刷脸登录功能
Odoo integrated plausible embedded code monitoring platform
spark调优(三):持久化减少二次查询
Leetcode-136- number that appears only once (solve with XOR)
作为Android开发程序员,android高级面试
记录Servlet学习时的一次乱码
Introduction to ThinkPHP URL routing
数据中台落地实施之法
Opportunity interview experience summary
The team of East China Normal University proposed the systematic molecular implementation of convolutional neural network with DNA regulation circuit
Imitate the choice of enterprise wechat conference room
Continuous creation depends on it!
Deep listening array deep listening watch
Laravel 中config的用法
【Android -- 数据存储】使用 SQLite 存储数据








