Python日期时间函数:如何对日期和时间进行处理
Python是一门开源高级编程语言,面向对象,具有简单易学的语法特点以及丰富的库函数,能够支持多种编程范式,因此得以被广泛应用于科学计算、数据处理、网络编程、Web开发、人工智能等领域。在Python编程中,日期和时间的处理是很常见的需求,因此这里我们将介绍一些Python常用的日期和时间函数。
Python内置的datetime库提供了各种日期和时间处理函数,包括日期表示、时间表示、时间差计算、日期格式化等功能。这里我们将分别介绍如下几个子模块:
1. datetime模块
datetime模块是Python内置的时间和日期处理模块,它提供了日期类(date)、时间类(time)和日期时间类(datetime)三种基本数据类型,可以对日期和时间进行各种运算和比较操作。
(1)获取当前日期和时间
要获取当前日期和时间,可以使用datetime模块中的datetime类中的now()方法,该方法可以返回当前日期和时间的datetime类型对象。
import datetime now = datetime.datetime.now() print(now)
输出结果:2019-10-30 09:58:31.035207
(2)获取日期和时间的各个部分
datetime对象中包含了日期和时间的各个部分,包括年、月、日、时、分、秒等。通过访问datetime对象的属性可以获取这些部分的值,例如:
now = datetime.datetime.now() year = now.year # 年 month = now.month # 月 day = now.day # 日 hour = now.hour # 时 minute = now.minute # 分 second = now.second # 秒 print(year, month, day, hour, minute, second)
输出结果:2019 10 30 10 5 37
(3)日期和时间的计算
对日期和时间进行加减运算是常见的需求。datetime对象支持加减 timedelta 类型对象进行计算。timedelta用于表示两个日期或时间之间的时间差。
now = datetime.datetime.now() delta_hours = datetime.timedelta(hours=2) # 2小时 future = now + delta_hours # 当前时间加2小时 print(future) delta_days = datetime.timedelta(days=7) # 7天 future = now + delta_days # 当前时间加7天 print(future)
输出结果:
2019-10-30 12:05:37.015103
2019-11-06 10:05:37.015103
(4)日期和时间的格式化
datetime对象可以通过strftime()方法来格式化输出。strftime()方法根据指定的格式字符串输出日期和时间的字符串表示。
now = datetime.datetime.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 %H:%M:%S.%f"))
输出结果:
2019-10-30 09:42:40
20191030_094240
2019-10-30 09:42:40.296163
2. time模块
time模块是Python内置的时间处理模块,也就是datetime模块中time类的子模块,它能够提供更加精细的时间处理功能。time模块主要包括了几个函数,包括获取时间戳、休眠等函数。
(1)获取当前时间戳
时间戳是指格林威治时间1970年1月1日0时0分0秒到当前时间的总秒数。time模块中的time()函数可以获取当前的时间戳。
import time timestamp = time.time() print(int(timestamp))
输出结果:1572380712
(2)日期和时间的格式化
strftime()函数除了可以格式化datetime对象,还可以格式化struct_time对象。struct_time对象是一个元组类型,包含了年、月、日、时、分、秒、星期几等信息。time模块中的gmtime()函数和localtime()函数分别可以获取格林威治时间和本地时间对应的struct_time对象。
import time
t = time.localtime() # 本地时间的struct_time对象
print(time.strftime("%Y-%m-%d %H:%M:%S", t))
t = time.gmtime() # 格林威治时间的struct_time对象
print(time.strftime("%Y-%m-%d %H:%M:%S", t))
输出结果:
2019-10-30 10:22:15
2019-10-30 02:22:15
结合datetime模块中的日期和时间格式化函数,我们可以将struct_time对象转换为datetime对象,然后进行加减、比较等操作。
3. calendar模块
calendar模块是Python内置的日历处理模块,提供了各种日历相关的函数。它能够满足一些日历方面的需求,比如说跨年计算、月历获取、年历获取等。此处只介绍月历和年历的获取功能。
(1)获取当月月历
calendar模块中的monthcalendar()函数用于获取指定年份和月份的日历矩阵,返回一个二维列表。其中每行表示一周,0表示该日期不在当前月份内,其他数字表示该日期在当前月份内。
import calendar
year = 2019
month = 10
matrix = calendar.monthcalendar(year, month)
for i in range(len(matrix)):
for j in range(len(matrix[0])):
if matrix[i][j] == 0:
print(" ", end=" ")
else:
print("{:2d}".format(matrix[i][j]), end=" ")
print()
输出结果:
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
(2)获取某年年历
calendar模块中的yeardays2calendar()函数用于获取指定年份的日历矩阵,返回一个三维列表。其中每个二维列表代表一个月份,每个月份列表的元素是一个三元组。三元组的 个元素是日期(这表示一整年中的顺序编号),第二个元素是该日期所属的星期数,第三个元素是该日期是否在当前月份内。
import calendar
year = 2019
matrix = calendar.yeardays2calendar(year, 3)
for month in matrix:
for week in month:
for day in week:
if day[2] == 0:
print(" ", end="")
else:
print("{:2d}".format(day[2]), end="")
print()
输出结果:
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
1 2 3
4 5 6 7 8 9
