python之time模块:获取当前时间
2023-09-11 14:19:57 时间
time模块举例
1 import time 2 3 # 获取当前时间戳 4 t = time.time() 5 print('1)获取当前时间戳:', t) 6 7 # 当前时间的struct_time形式 8 t = time.localtime() 9 print('2)当前时间的struct_time形式:', t) 10 print('3)当前年份:', t.tm_year) 11 12 # 当前时间的字符串形式 13 t = time.ctime() 14 print('4)当前时间的字符串形式:', t) 15 16 # 当前时间自定义格式的字符串 17 t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 18 print('5)当前时间自定义格式的字符串:', t) 19 20 # 将字符串转换为time对象,并改变时间格式 21 str = '20161228080000' 22 time_str = time.strptime(str, '%Y%m%d%H%M%S') 23 print("6)字符串转换成time对象:", time_str) 24 t = time.strftime('%Y-%m-%d %H:%M:%S', time_str) 25 print("7)转换时间格式:", t)
运行结果:
1 1)获取当前时间戳: 1502420198.8323882 2 2)当前时间的struct_time形式: time.struct_time(tm_year=2017, tm_mon=8, tm_mday=11, 3 tm_hour=10, tm_min=56, tm_sec=38, tm_wday=4, tm_yday=223, tm_isdst=0) 4 3)当前年份: 2017 5 4)当前时间的字符串形式: Fri Aug 11 10:56:38 2017 6 5)当前时间自定义格式的字符串: 2017-08-11 10:56:38 7 6)字符串转换成time对象: time.struct_time(tm_year=2016, tm_mon=12, tm_mday=28, 8 tm_hour=8, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=363, tm_isdst=-1) 9 7)转换时间格式: 2016-12-28 08:00:00
相关文章
- Python中python-nmap模块的使用
- python内置模块之unittest测试(五)
- 如何查看python某个指令的参数,直接在相应的环境输入 -h
- 安装PyMysql报错 python setup.py egg_info Check the logs for full command output.
- Python 字符串常用函数
- Python错误:TypeError: string indices must be integers
- Word处理控件Aspose.Words功能演示:使用 Python 将 Word 文档转换为 TIFF
- python自定义包或模块在Linux服务器导入错误的解决办法
- 65 python - 模块中的__all__
- CSDN博客第一期订阅专栏:跟“风云卫星”数据工程师学Python
- Python爬虫技术--基础篇--内建模块itertools,contextlib和urllib
- 【Python】【PyPI】twine模块打包python项目上传pypi
- Python编程-基础知识-python项目包和文件的管理以及如何引用相对路径的包和模块
- Python os模块
- Python random模块(获取随机数)常用方法和使用例子
- python之模块ftplib(FTP协议的客户端)
- python之模块datetime详解
- python自动化运维常用模块安装
- Python图像处理库:PIL中Image,ImageDraw等基本模块介绍
- python搜索模块如何查询
- Python 教程之如何使用 matplotlib 在 python 中绘制数学函数
- 小学生python游戏编程arcade----坦克大战2
- 【LeetCode】【Python题解】Reverse Integer
- 数据结构和算法:Python实现二分查找(Binary_search)
- Python删除列表中的空格
- 学习笔记(35):Python网络编程&并发编程-协程(yield,greenlet,gevent模块)
- python中os模块的常用方法