Python 提供了 日期和时间模块用来处理日期和时间,还可以用于格式化日期和时间等常见功能。
- 时间间隔是以秒为单位的浮点小数。
- 每个时间戳都以自从 1970 年 1 月 1 日午夜(历元)经过了多长时间来表示。
一、time模块使用
Time 模块包含了大量内置函数,既有时间处理的,也有转换时间格式的。
1、获取当前时间
1
2
3
4
5
6
7
8
9
10
11
|
import time # 获取时间戳 返回的是浮点型 # 作用 用来计算时间差 print (time.time()) # 1676775733.1080997 # 获取当地时间 返回的是结构化时间 print (time.localtime()) # 获取UTC时间 返回的还是结构化时间 比中国时间少8小时 print (time.gmtime()) |
1.1 时间元组
时间元组:Python函数用一个元组装起来的9组数字处理时间。
上面也就是 struct_time 元组。这种结构具有如下属性:
2、格式化时间
2.1 获取格式化的时间
1
2
3
4
5
6
7
|
import time # 获取本地时间 localtime = time.asctime(time.localtime(time.time())) #localtime = time.asctime(time.localtime()) print ( "本地时间为 :" , localtime) ## 本地时间为 : Mon Feb 20 20:49:10 2023 |
2.2 格式化时间转换
使用 time 模块的 strftime 方法来格式化日期,通过 strptime方法来格式化字符串。
1
2
3
4
5
6
7
8
9
10
|
# 时间转换为格式字符串 print (time.strftime( "%Y-%m-%d %H:%M:%S" , time.localtime())) print (time.strftime( "%Y-%m-%d %H:%M:%S" , time.localtime(time.time()))) # 格式字符串转换为时间 str = "2023-02-14 05:20:00" print (time.strptime( str , "%Y-%m-%d %H:%M:%S" )) # 格式字符串转换为时间戳 print (time.mktime(time.strptime( str , "%Y-%m-%d %H:%M:%S" ))) |
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 当前时区的名称
- %% %号本身
二、datetime模块
datetime模块提供了对时间和日期进行操作的功能。
datatime模块通过date类、time类、datetime类、timedelta类、timezone类等来获取或者操作时间、日期。
与time模块相比,datetime模块提供的接口更直观、易用,功能也更加强大。
1、获取当前时间
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
from datetime import datetime # 获取时间 获取当前时间 并且返回的是格式化字符时间 now = datetime.now() print (now) print ( type (now)) print (now.year) print (now.month) print (now.day) print (now.hour) print (now.minute) print (now.second) print (now.microsecond) # 手动指定时间 print (datetime( 2023 , 2 , 20 , 20 , 50 , 00 )) # 替换某个时间单位的值 print (now.replace(year = 2024 )) d1 = datetime.now() d2 = datetime( 2024 , 2 , 20 , 20 , 50 , 00 ) print (d1 - d2) |
2、格式化时间
(1)datetime 转 字符串,通过 strftime()函数。
1
2
3
4
5
6
7
8
9
10
11
|
from datetime import datetime now = datetime.now() # 强制转换字符串 print ( str (now)) # 格式化日期 转换 字符串 print (now.strftime( "%Y/%m/%d %H:%M:%S" )) print (now.strftime( "%Y-%m-%d %H:%M:%S" )) print (now.strftime( "%Y/%m/%d" )) print (now.strftime( "%Y-%m-%d" )) |
(2)字符串 转 datetime对象,通过 strptime()函数。
1
2
3
4
|
dates = [ '2022-02-20 20:23:35' , '2024-02-20 20:23:35' ] datelist = [datetime.strptime(i, "%Y-%m-%d %H:%M:%S" ) for i in dates] print (datelist) # [datetime.datetime(2022, 2, 20, 20, 23, 35), datetime.datetime(2024, 2, 20, 20, 23, 35)] |
(3)parse() 解析字符串
将字符串形式的日期解析成 datetime对象。
1
2
3
4
5
6
7
8
|
# 需要安装 pip install python-dateutil from dateutil.parser import parse print (parse( '2/20/2023' )) print (parse( '2023-2-20' )) print (parse( '2023.2.20' )) print (parse( '2023 2 4' )) print (parse( '2023, 6, 2' )) |
3、计算时间间隔
时间间隔 timedelta
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
import datetime now = datetime.datetime.now() print (now) # 3天后 dt1 = now + datetime.timedelta( 3 ) print (dt1) # 3天前 dt2 = now + datetime.timedelta( - 3 ) print (dt2) # 3小时30秒后 dt3 = now + datetime.timedelta(hours = 3 , seconds = 30 ) print (dt3) |
三、calendar模块
calendar模块的函数都是日历相关的,例如打印某月的字符月历。
calendar 特点:
- calendar 模块是基于datetime.date.weekday()对计算每一周的周数
- calendar 默认星期一是每一周的第一天,星期天是一周的最后一天
- calendar 提供calendar.setfirstweeksday()来更改指定星期几为一周的第一天
1、打印日历
1
2
3
4
5
6
7
|
import calendar # 打印指定某年的月历 print (calendar.month( 2023 , 5 )) # 打印2023年历 # print(calendar.calendar(2023)) |
上面示例了一些常见的操作,下面参考文章(感谢博主)中详细列举的各模块之间的属性和方法,这里就不重复造轮子啦。
以上就是一文带你吃透Python中的日期时间模块的详细内容,更多关于Python日期时间模块的资料请关注服务器之家其它相关文章!
原文链接:https://blog.csdn.net/qq_42402854/article/details/129131487