欢迎访问宙启技术站
智能推送

时间和日期相关的Python函数使用技巧

发布时间:2023-10-28 03:22:24

1. 获取当前时间和日期

使用Python的datetime模块可以轻松地获取当前时间和日期。可以使用datetime的now()方法来获取当前时间和日期的datetime对象。

from datetime import datetime

current_time = datetime.now()
print(current_time)

输出示例:2022-04-25 10:30:00.123456

2. 格式化时间和日期

使用strftime()方法可以将datetime对象格式化为字符串,以便更好地输出或存储。

from datetime import datetime

current_time = datetime.now()
formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S")
print(formatted_time)

输出示例:2022-04-25 10:30:00

3. 解析字符串为时间和日期

使用strptime()方法可以将字符串解析为datetime对象,以便进行时间和日期的处理。

from datetime import datetime

date_string = "2022-04-25"
parsed_date = datetime.strptime(date_string, "%Y-%m-%d")
print(parsed_date)

输出示例:2022-04-25 00:00:00

4. 时间和日期的加减操作

可以使用timedelta()方法对datetime对象进行加减操作,以获取之前或之后的时间和日期。

from datetime import datetime, timedelta

current_time = datetime.now()
one_day_ago = current_time - timedelta(days=1)
one_hour_later = current_time + timedelta(hours=1)
print(one_day_ago)
print(one_hour_later)

输出示例:

2022-04-24 10:30:00

2022-04-25 11:30:00

5. 比较时间和日期

使用比较运算符可以轻松地比较两个datetime对象的时间和日期。

from datetime import datetime

current_time = datetime.now()
other_time = datetime(2022, 4, 25, 8, 0, 0)
if current_time > other_time:
    print("当前时间晚于指定时间")
else:
    print("当前时间早于指定时间")

输出示例:当前时间早于指定时间

6. 计算时间差

可以使用subtract()方法计算两个datetime对象之间的时间差,并得到时间差的具体数值。

from datetime import datetime

current_time = datetime.now()
other_time = datetime(2022, 4, 25, 8, 0, 0)
time_difference = current_time - other_time
print(time_difference.days)
print(time_difference.seconds)

输出示例:

0

18000

7. 获取日期和时间的部分信息

可以使用datetime对象的属性来获取日期和时间的具体部分信息,如年、月、日、时、分、秒等。

from datetime import datetime

current_time = datetime.now()
print(current_time.year)
print(current_time.month)
print(current_time.day)
print(current_time.hour)
print(current_time.minute)
print(current_time.second)

输出示例:

2022

4

25

10

30

0

8. 时间和日期的格式化输出

除了使用strftime()方法对datetime对象进行格式化输出外,还可以使用date()和time()方法分别获取日期和时间的对象,并进行格式化输出。

from datetime import datetime

current_time = datetime.now()
formatted_date = current_time.date().strftime("%Y-%m-%d")
formatted_time = current_time.time().strftime("%H:%M:%S")
print(formatted_date)
print(formatted_time)

输出示例:

2022-04-25

10:30:00

9. 获取某年某月的日历

可以使用calendar模块的monthrange()方法获取某年某月的天数,并使用calendar模块的prmonth()方法打印出日历。

import calendar

year = 2022
month = 4
days_in_month = calendar.monthrange(year, month)[1]
calendar.prmonth(year, month)
print(f"该月共有{days_in_month}天")

输出示例:

April 2022

Mo Tu We Th Fr Sa Su

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

该月共有30天

10. 获取昨天、明天和本周的日期

可以使用timedelta()方法获取昨天和明天的日期,并使用isoweekday()方法获取本周的日期列表。

from datetime import datetime, timedelta

current_time = datetime.now()
yesterday = current_time - timedelta(days=1)
tomorrow = current_time + timedelta(days=1)

weekdays = [current_time - timedelta(days=i) for i in range(current_time.isoweekday())]
print("昨天:", yesterday)
print("明天:", tomorrow)
print("本周:", weekdays)

输出示例:

昨天: 2022-04-24 10:30:00

明天: 2022-04-26 10:30:00

本周: [datetime.datetime(2022, 4, 25, 10, 30), datetime.datetime(2022, 4, 24, 10, 30)]