Python time(时间模块)

举报
Yuchuan 发表于 2020/01/17 19:29:43 2020/01/17
【摘要】 在Python中,通常有这三种方式来表示时间:时间戳、元组(struct_time)、格式化的时间字符串.

和时间有关系的我们就要用到时间模块。在使用模块之前,应该首先导入这个模块。

# time 模块的常用方法
import time

# 1、(线程)推迟指定的时间运行,单位为秒。
time.sleep(3)
# 2、获取当前时间戳
tim_t = time.time()
print(tim_t)

结果:

等待3秒后执行结果:
D:\YuchuanProjectData\PythonProject\venv\Scripts\python.exe D:/YuchuanProjectData/PythonProject/YuchuanDemo006.py
1579252953.7368343
Process finished with exit code 0

在Python中,通常有这三种方式来表示时间:时间戳、元组(struct_time)、格式化的时间字符串:

(1)时间戳(timestamp) :通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。我们运行“type(time.time())”,返回的是float类型。

(2)格式化的时间字符串(Format String): ‘1999-12-06’

print(type(time.time()))

结果:

D:\YuchuanProjectData\PythonProject\venv\Scripts\python.exe D:/YuchuanProjectData/PythonProject/YuchuanDemo006.py
<class 'float'>
Process finished with exit code 0

python中时间日期格式化符号:

%y 两位数的年份表示(00-99)
%Y 四位数的年份表示(000-9999)
%m 月份(01-12)
%d 月内中的一天(0-31)
%H 24小时制小时数(0-23)
%I 12小时制小时数(01-12)
%M 分钟数(00=59)
%S 秒(00-59)
%a 本地简化星期名称
%A 本地完整星期名称
%b 本地简化的月份名称
%B 本地完整的月份名称
%c 本地相应的日期表示和时间表示
%j 年内的一天(001-366)
%p 本地A.M.或P.M.的等价符
%U 一年中的星期数(00-53)星期天为星期的开始
%w 星期(0-6),星期天为星期的开始
%W 一年中的星期数(00-53)星期一为星期的开始
%x 本地相应的日期表示
%X 本地相应的时间表示
%Z 当前时区的名称
%% %号本身

(3)元组(struct_time) :struct_time元组共有9个元素共九个元素:(年,月,日,时,分,秒,一年中第几周,一年中第几天等)

索引(Index)属性(Attribute)值(Values)
0tm_year(年)比如2011
1tm_mon(月)1 - 12
2tm_mday(日)1 - 31
3tm_hour(时)0 - 23
4tm_min(分)0 - 59
5tm_sec(秒)0 - 60
6tm_wday(weekday)0 - 6(0表示周一)
7tm_yday(一年中的第几天)1 - 366
8tm_isdst(是否是夏令时)默认为0

首先,我们先导入time模块,来认识一下python中表示时间的几种格式:

# 导入时间模块
import time

# 时间戳
time_t = time.time()
print(time_t)
# 时间字符串
print(time.strftime("%Y-%m-%d %X"))
print(time.strftime("%Y-%m-%d %H-%M-%S"))
# 时间元组:localtime将一个时间戳转换为当前时区的struct_time
print(time.localtime(time_t))

结果:

D:\YuchuanProjectData\PythonProject\venv\Scripts\python.exe D:/YuchuanProjectData/PythonProject/YuchuanDemo006.py
1579255940.0655744
2020-01-17 18:12:20
2020-01-17 18-12-20
time.struct_time(tm_year=2020, tm_mon=1, tm_mday=17, tm_hour=18, tm_min=12, tm_sec=20, tm_wday=4, tm_yday=17, tm_isdst=0)
Process finished with exit code 0

小结:时间戳是计算机能够识别的时间;时间字符串是人能够看懂的时间;元组则是用来操作时间的

几种格式之间的转换

1579256059266971.png

import time

# 时间戳-->结构化时间
# time.gmtime(时间戳)    #UTC时间,与英国伦敦当地时间一致
# time.localtime(时间戳) #当地时间。例如我们现在在北京执行这个方法:与UTC时间相差8小时,UTC时间+8小时 = 北京时间
time_t = time.time()
time_gm = time.gmtime(time_t)
print(time_gm)
time_loc = time.localtime(time_t)
print(time_loc)

# 结构化时间-->时间戳 
# time.mktime(结构化时间)
time.sleep(3)
time_tuple = time.localtime(time.time())
print(time.mktime(time_tuple))

结果:

D:\YuchuanProjectData\PythonProject\venv\Scripts\python.exe D:/YuchuanProjectData/PythonProject/YuchuanDemo006.py
time.struct_time(tm_year=2020, tm_mon=1, tm_mday=17, tm_hour=10, tm_min=22, tm_sec=45, tm_wday=4, tm_yday=17, tm_isdst=0)
time.struct_time(tm_year=2020, tm_mon=1, tm_mday=17, tm_hour=18, tm_min=22, tm_sec=45, tm_wday=4, tm_yday=17, tm_isdst=0)
1579256568.0
Process finished with exit code 0


import time

# 结构化时间-->字符串时间
# time.strftime("格式定义","结构化时间")  结构化时间参数若不传,则显示当前时间
print(time.strftime("%Y-%m-%d %X"))
print(time.strftime("%Y-%m-%d", time.localtime(1600000000)))

# 字符串时间-->结构化时间
# time.strptime(时间字符串,字符串对应格式)
print(time.strptime("2020-01-17", "%Y-%m-%d"))
print(time.strptime("01/17/2020", "%m/%d/%Y"))

结果:

D:\YuchuanProjectData\PythonProject\venv\Scripts\python.exe D:/YuchuanProjectData/PythonProject/YuchuanDemo006.py
2020-01-17 18:31:32
2020-09-13
time.struct_time(tm_year=2020, tm_mon=1, tm_mday=17, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=4, tm_yday=17, tm_isdst=-1)
time.struct_time(tm_year=2020, tm_mon=1, tm_mday=17, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=4, tm_yday=17, tm_isdst=-1)
Process finished with exit code 0

1579257205633246.png


import time

# 结构化时间 --> %a %b %d %H:%M:%S %Y串
# time.asctime(结构化时间) 如果不传参数,直接返回当前时间的格式化串
print(time.asctime(time.localtime(1600000000)))
print(time.asctime())

# 时间戳 --> %a %b %d %H:%M:%S %Y串
# time.ctime(时间戳)  如果不传参数,直接返回当前时间的格式化串
print(time.ctime())
print(time.ctime(1700000000))

结果:

D:\YuchuanProjectData\PythonProject\venv\Scripts\python.exe D:/YuchuanProjectData/PythonProject/YuchuanDemo006.py
Sun Sep 13 20:26:40 2020
Fri Jan 17 18:41:29 2020
Fri Jan 17 18:41:29 2020
Wed Nov 15 06:13:20 2023
Process finished with exit code 0

计算时间差

import time

true_time = time.mktime(time.strptime('2020-01-11 08:30:00', '%Y-%m-%d %H:%M:%S'))
time_now = time.mktime(time.strptime('2020-09-12 11:00:00', '%Y-%m-%d %H:%M:%S'))
dif_time = time_now - true_time
struct_time = time.gmtime(dif_time)
print('过去了%d年%d月%d天%d小时%d分钟%d秒' % (struct_time.tm_year - 1970, struct_time.tm_mon - 1,
                                   struct_time.tm_mday - 1, struct_time.tm_hour,
                                   struct_time.tm_min, struct_time.tm_sec))

结果:

D:\YuchuanProjectData\PythonProject\venv\Scripts\python.exe D:/YuchuanProjectData/PythonProject/YuchuanDemo006.py
过去了0年8月2天2小时30分钟0秒
Process finished with exit code 0

datetime模块

1.datetime.now() # 获取当前datetime

datetime.utcnow() # 获取当前格林威治时间

from datetime import datetime

# 获取当前datetime
print(datetime.now())
# 获取当前格林威治时间
print(datetime.utcnow())

"""
from datetime import datetime

#获取当前本地时间
a=datetime.now()
print('当前日期:',a)
#获取当前世界时间
b=datetime.utcnow()
print('世界时间:',b)
"""

结果:

D:\YuchuanProjectData\PythonProject\venv\Scripts\python.exe D:/YuchuanProjectData/PythonProject/YuchuanDemo006.py
2020-01-17 18:54:11.325690
2020-01-17 10:54:11.325690
Process finished with exit code 0

2.datetime(2017, 5, 23, 12, 20) # 用指定日期时间创建datetime

from datetime import datetime

# 用指定日期创建
c = datetime(2020, 1, 17, 12, 20)
print('指定日期:', c)

结果:

D:\YuchuanProjectData\PythonProject\venv\Scripts\python.exe D:/YuchuanProjectData/PythonProject/YuchuanDemo006.py
指定日期: 2020-01-17 12:20:00
Process finished with exit code 0

3.将以下字符串转换成datetime类型:

'2020/9/30'
'2020年9月30日星期六'
'2020年9月30日星期六8时42分24秒'
'9/30/2020'
'9/30/2020 8:42:50 '

from datetime import datetime

d = datetime.strptime('2020/9/30', '%Y/%m/%d')
print(d)
e = datetime.strptime('2020年9月30日星期六', '%Y年%m月%d日星期六')
print(e)
f = datetime.strptime('2020年9月30日星期六8时42分24秒', '%Y年%m月%d日星期六%H时%M分%S秒')
print(f)
g = datetime.strptime('9/30/2020', '%m/%d/%Y')
print(g)
h = datetime.strptime('9/30/2020 8:42:50 ', '%m/%d/%Y %H:%M:%S ')
print(h)

结果:

D:\YuchuanProjectData\PythonProject\venv\Scripts\python.exe D:/YuchuanProjectData/PythonProject/YuchuanDemo006.py
2020-09-30 00:00:00
2020-09-30 00:00:00
2020-09-30 08:42:24
2020-09-30 00:00:00
2020-09-30 08:42:50
Process finished with exit code 0

4.将以下datetime类型转换成字符串:

2020年9月28日星期4,10时3分43秒
Saturday, September 30, 2020
9/30/2020 9:22:17 AM
September 30, 2020

时间字符串格式化

from datetime import datetime
import locale

locale.setlocale(locale.LC_CTYPE, 'chinese')
i = datetime(2020, 9, 28, 10, 3, 43)
print(i.strftime('%Y年%m月%d日%A,%H时%M分%S秒'))
j = datetime(2020, 9, 30, 10, 3, 43)
print(j.strftime('%A,%B %d,%Y'))
k = datetime(2020, 9, 30, 9, 22, 17)
print(k.strftime('%m/%d/%Y %I:%M:%S%p'))
l = datetime(2020, 9, 30)
print(l.strftime('%B %d,%Y'))

结果:

D:\YuchuanProjectData\PythonProject\venv\Scripts\python.exe D:/YuchuanProjectData/PythonProject/YuchuanDemo006.py
2020年09月28日Monday,10时03分43秒
Wednesday,September 30,2020
09/30/2020 09:22:17AM
September 30,2020
Process finished with exit code 0

5.用系统时间输出以下字符串:

今天是2020年1月17日
今天是这周的第?天 
今天是今年的第?天 
今周是今年的第?周 
今天是当月的第?天

from datetime import datetime
import locale

locale.setlocale(locale.LC_CTYPE, 'chinese')
# 获取当前系统时间
m = datetime.now()
print(m.strftime('今天是%Y年%m月%d日'))
print(m.strftime('今天是这周的第%w天'))
print(m.strftime('今天是今年的第%j天'))
print(m.strftime('今周是今年的第%W周'))
print(m.strftime('今天是当月的第%d天'))

结果:

D:\YuchuanProjectData\PythonProject\venv\Scripts\python.exe D:/YuchuanProjectData/PythonProject/YuchuanDemo006.py
今天是2020年01月17日
今天是这周的第5天
今天是今年的第017天
今周是今年的第02周
今天是当月的第17天
Process finished with exit code 0


【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

举报
请填写举报理由
0/200