轻松学会Python时间模块 python time模块日期运算
bigegpt 2024-10-08 00:48 6 浏览
Python中表示时间的三种方式
- 时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。Python 中时间戳是float类型。时间戳是一个秒数
- 格式化的时间字符串, 如: YYYY-mm-dd。
- 元组(struct_time): struct_time元组共有9个元素:(年,月,日,时,分,秒,一年中第几周,一年中第几天,夏令时) 。主要是便于操作某一个时间年/月/日等.
Python中time主要逻辑关系如下图:
时间模块的常用功能
导入模块
import time
获取时间戳
time.time()
# 1701054229.5299459
localtime([secs])方法: 时间戳 -> 本地结构化时间, 将时间戳转换为当前时区的 struct_time, 无参数是, 则为当前时间
t = time.time()
# 1701054229.5299459
time.localtime(t)
# time.struct_time(tm_year=2023, tm_mon=11, tm_mday=27, tm_hour=13, tm_min=26, tm_sec=54, tm_wday=0, tm_yday=331, tm_isdst=0)
gmtime([secs]): 时间戳 -> utc 标准结构化时间(无时区)
time.gmtime(time.time())
# time.struct_time(tm_year=2023, tm_mon=11, tm_mday=30, tm_hour=1, tm_min=9, tm_sec=57, tm_wday=3, tm_yday=334, tm_isdst=0)
mktime 方法: 结构化时间或时间元组 -> 时间戳
# mktime(t: tuple[int, int, int, int, int, int, int, int, int] | struct_time)
time.mktime(time.localtime())
# 1701064193.0
asctime 方法: 将结构化时间或时间元组转->人类可读的字符串格式
# asctime(t: tuple[int, int, int, int, int, int, int, int, int] | struct_time)
time.asctime(time.localtime())
# Mon Nov 27 13:55:44 2023
ctime(secs): 将时间戳转-> 人类可读的字符串
time.ctime(time.time())
# Mon Nov 27 13:59:00 2023
strftime(format[, t])方法: 结构化时间 -> 格式化时间字符串, 把一个代表时间的元组或者struct_time(如time.localtime()和time.gmtime()返回)转化为格式化的时间字符串
time.strftime("%Y-%m-%d %X", time.localtime())
# 2023-11-27 14:00:05
strptime() 格式化时间字符串 -> 结构化时间, 文末附格式字符串占位符表
time.strptime('2023-11-05 15:27:08', '%Y-%m-%d %X')
# time.struct_time(tm_year=2023, tm_mon=11, tm_mday=5, tm_hour=15, tm_min=27, tm_sec=8, tm_wday=6, tm_yday=309, tm_isdst=-1)
注意: clock(), 该函数在 Python3 中已被删除, 如果需要模拟该功能, 可使用以下代码
import time
def clock():
return time.time()
# 使用示例
start_time = clock()
# 在这里执行你的代码
end_time = clock()
elapsed_time = end_time - start_time
print(" elapsed time:", elapsed_time)
占位符含义
Specifier | Replaced By | Example |
%a | Abbreviated weekday name | Sun |
%A | Full weekday name | Sunday |
%b | Abbreviated month name | Mar |
%B | Full month name | March |
%c | Date and time representation | Sun Aug 19 02:56:02 2012 |
%d | Day of the month (01-31) | 19 |
%H | Hour in 24h format (00-23) | 14 |
%I | Hour in 12h format (01-12) | 05 |
%j | Day of the year (001-366) | 231 |
%m | Month as a decimal number (01-12) | 08 |
%M | Minute (00-59) | 55 |
%p | AM or PM designation | PM |
%S | Second (00-61) | 02 |
%U | Week number with the first Sunday as the first day of week one (00-53) | 33 |
%w | Weekday as a decimal number with Sunday as 0 (0-6) | 4 |
%W | Week number with the first Monday as the first day of week one (00-53) | 34 |
%x | Date representation | 08/19/12 |
%X | Time representation | 02:50:06 |
%y | Year, last two digits (00-99) | 01 |
%Y | Year | 2012 |
%Z | Timezone name or abbreviation | CDT |
%% | A % sign | % |
相关推荐
- pyproject.toml到底是什么东西?(py trim)
-
最近,在Twitter上有一个Python项目的维护者,他的项目因为构建失败而出现了一些bug(这个特别的项目不提供wheel,只提供sdist)。最终,发现这个bug是由于这个项目使用了一个pypr...
- BDP服务平台SDK for Python3发布(bdp数据平台)
-
下载地址https://github.com/imysm/opends-sdk-python3.git说明最近在开发和bdp平台有关的项目,用到了bdp的python的sdk,但是官方是基于p...
- Python-for-Android (p4a):(python-for-android p4a windows)
-
一、Python-for-Android(p4a)简介Python-for-Android(p4a),一个强大的开发工具,能够将你的Python应用程序打包成可在Android设备上运行...
- Qt for Python—Qt Designer 概览
-
前言本系列第三篇文章(QtforPython学习笔记—应用程序初探)、第四篇文章(QtforPython学习笔记—应用程序再探)中均是使用纯代码方式来开发PySide6GUI应用程序...
- Python:判断质数(jmu-python-判断质数)
-
#Python:判断质数defisPrime(n):foriinrange(2,n):ifn%i==0:return0re...
- 为什么那么多人讨厌Python(为什么python这么难)
-
Python那么棒,为什么那么多人讨厌它呢?我整理了一下,主要有这些原因:用缩进替代大括号许多人抱怨Python完全依赖于缩进来创建代码块,代码多一点就很难看到函数在哪里结束,那么你就需要把一个函数拆...
- 一文了解 Python 中带有 else 的循环语句 for-else/while-else
-
在本文中,我们将向您介绍如何在python中使用带有else的for/while循环语句。可能许多人对循环和else一起使用感到困惑,因为在if-else选择结构中else正常...
- python的numpy向量化语句为什么会比for快?
-
我们先来看看,python之类语言的for循环,和其它语言相比,额外付出了什么。我们知道,python是解释执行的。举例来说,执行x=1234+5678,对编译型语言,是从内存读入两个shor...
- 开眼界!Python遍历文件可以这样做
-
来源:【公众号】Python技术Python对于文件夹或者文件的遍历一般有两种操作方法,一种是至二级利用其封装好的walk方法操作:import osfor root,d...
- 告别简单format()!Python Formatter类让你的代码更专业
-
Python中Formatter类是string模块中的一个重要类,它实现了Python字符串格式化的底层机制,允许开发者创建自定义的格式化行为。通过深入理解Formatter类的工作原理和使用方法,...
- python学习——038如何将for循环改写成列表推导式
-
在Python里,列表推导式是一种能够简洁生成列表的表达式,可用于替换普通的for循环。下面是列表推导式的基本语法和常见应用场景。基本语法result=[]foriteminite...
- 详谈for循环和while循环的区别(for循环语句与while循环语句有什么区别)
-
初九,潜龙勿用在刚开始使用python循环语句时,经常会遇到for循环和while循环的混用,不清楚该如何选择;今天就对这2个循环语句做深入的分析,让大家更好地了解这2个循环语句以方便后续学习的加深。...
- Python编程基础:循环结构for和while
-
Python中的循环结构包括两个,一是遍历循环(for循环),一是条件循环(while循环)。遍历循环遍历循环(for循环)会挨个访问序列或可迭代对象的元素,并执行里面的代码块。foriinra...
- 学习编程第154天 python编程 for循环输出菱形图
-
今天学习的是刘金玉老师零基础Python教程第38期,主要内容是python编程for循环输出菱形※。(一)利用for循环输出菱形形状的*号图形1.思路:将菱形分解为上下两个部分三角形图案,分别利用...
- python 10个堪称完美的for循环实践
-
在Python中,for循环的高效使用能显著提升代码性能和可读性。以下是10个堪称完美的for循环实践,涵盖数据处理、算法优化和Pythonic编程风格:1.遍历列表同时获取索引(enumerate...
- 一周热门
- 最近发表
-
- pyproject.toml到底是什么东西?(py trim)
- BDP服务平台SDK for Python3发布(bdp数据平台)
- Python-for-Android (p4a):(python-for-android p4a windows)
- Qt for Python—Qt Designer 概览
- Python:判断质数(jmu-python-判断质数)
- 为什么那么多人讨厌Python(为什么python这么难)
- 一文了解 Python 中带有 else 的循环语句 for-else/while-else
- python的numpy向量化语句为什么会比for快?
- 开眼界!Python遍历文件可以这样做
- 告别简单format()!Python Formatter类让你的代码更专业
- 标签列表
-
- mybatiscollection (79)
- mqtt服务器 (88)
- keyerror (78)
- c#map (65)
- xftp6 (83)
- bt搜索 (75)
- c#var (76)
- xcode-select (66)
- mysql授权 (74)
- 下载测试 (70)
- linuxlink (65)
- pythonwget (67)
- androidinclude (65)
- libcrypto.so (74)
- linux安装minio (74)
- ubuntuunzip (67)
- vscode使用技巧 (83)
- secure-file-priv (67)
- vue阻止冒泡 (67)
- jquery跨域 (68)
- php写入文件 (73)
- kafkatools (66)
- mysql导出数据库 (66)
- jquery鼠标移入移出 (71)
- 取小数点后两位的函数 (73)