Python 输出时间

在编程中,经常会需要获取、处理时间信息,比如记录日志、计时器或者操作数据库等。Python 提供了多个用于输出时间的模块和函数,本文将介绍这些内容,并给出相应的示例代码。
时间模块
Python 提供了 time、datetime 和 calendar 等模块来处理时间相关的操作。下面分别介绍这些模块的用法。
time 模块
time 模块提供了和时间有关的函数,包括获取当前时间、时间转换、时间延迟等。
time.time():返回当前时间的时间戳(从新纪元开始后的秒数)。time.localtime([seconds]):将时间戳转换为本地时间,或将一个时间戳转换为time.struct_time格式的时间。time.strftime(format, [tuple]):格式化时间。将time.struct_time格式的时间使用指定的格式转换为字符串。time.sleep(seconds):暂停当前程序执行指定的秒数。
下面是示例代码:
import time
# 获取当前时间的时间戳
timestamp = time.time()
print("当前时间戳:", timestamp)
# 将时间戳转换为本地时间
local_time = time.localtime(timestamp)
print("当前本地时间:", local_time)
# 格式化时间
formatted_time = time.strftime("%Y-%m-%d %H:%M:%S", local_time)
print("格式化时间:", formatted_time)
# 暂停 1 秒
time.sleep(1)
print("1 秒后的时间:", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
运行上述代码,输出如下:
当前时间戳: 1644941683.577518
当前本地时间: time.struct_time(tm_year=2022, tm_mon=2, tm_mday=15, tm_hour=10, tm_min=28, tm_sec=3, tm_wday=1, tm_yday=46, tm_isdst=0)
格式化时间: 2022-02-15 10:28:03
1 秒后的时间: 2022-02-15 10:28:04
datetime 模块
datetime 模块提供了更高级的时间操作功能,包括日期和时间的计算、格式化输出等。
datetime.datetime.now():获取当前的日期时间。datetime.datetime(year, month, day, hour, minute, second, microsecond):创建一个指定日期时间的对象。datetime.datetime.strftime(format):将datetime对象格式化为指定的字符串。datetime.datetime.strptime(date_string, format):将字符串解析为datetime对象。datetime.timedelta(days, seconds, microseconds, milliseconds, minutes, hours, weeks):表示一个时间间隔。
下面是示例代码:
import datetime
# 获取当前日期时间
now = datetime.datetime.now()
print("当前日期时间:", now)
# 创建指定日期时间的对象
custom_time = datetime.datetime(2022, 2, 15, 10, 30, 0)
print("自定义日期时间:", custom_time)
# 格式化输出
formatted_datetime = now.strftime("%Y-%m-%d %H:%M:%S")
print("格式化日期时间:", formatted_datetime)
# 解析字符串为 datetime 对象
parsed_datetime = datetime.datetime.strptime("2022-02-15 10:30:00", "%Y-%m-%d %H:%M:%S")
print("解析后的日期时间:", parsed_datetime)
# 时间计算
time_delta = datetime.timedelta(days=1, hours=3)
new_datetime = now + time_delta
print("增加时间间隔后的日期时间:", new_datetime)
运行上述代码,输出如下:
当前日期时间: 2022-02-15 10:30:00.296154
自定义日期时间: 2022-02-15 10:30:00
格式化日期时间: 2022-02-15 10:30:00
解析后的日期时间: 2022-02-15 10:30:00
增加时间间隔后的日期时间: 2022-02-16 13:30:00.296154
calendar 模块
calendar 模块提供了一些与日历相关的函数,例如判断某年是否是闰年、获取某月的日历等。
calendar.calendar(year, w=2, l=1, c=6):返回一个多行字符串格式的某年整年日历。calendar.isleap(year):判断某年是否是闰年。calendar.month(year, month, w=2, l=1):返回一个多行字符串格式的某年月历。
下面是示例代码:
import calendar
# 打印整年日历
year_calendar = calendar.calendar(2022)
print("2022 年整年日历:\n", year_calendar)
# 判断是否是闰年
is_leap = calendar.isleap(2022)
print("2022 年是否是闰年:", is_leap)
# 打印某年某月日历
month_calendar = calendar.month(2022, 2)
print("2022 年 2 月日历:\n", month_calendar)
运行上述代码,输出如下:
2022 年整年日历:
2022
January February March
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 1 2 3 4 5 6 1 2 3 4 5 6
3 4 5 6 7 8 9 7 8 9 10 11 12 13 7 8 9 10 11 12 13
10 11 12 13 14 15 16 14 15 16 17 18 19 20 14 15 16 17 18 19 20
17 18 19 20 21 22 23 21 22 23 24 25 26 27 21 22 23 24 25 26 27
24 25 26 27 28 29 30 28 28 29 30 31
April May June
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 1 1 2 3 4 5
3 4 5 6 7 8 9 2 3 4 5 6 7 8 6 7 8 9 10 11 12
10 11 12 13 14 15 16 9 10 11 12 13 14 15 13 14 15 16 17 18 19
17 18 19 20 21 22 23 16 17 18 19 20 21 22 20 21 22 23 24 25 26
24 25 26 27 28 29 30 23 24 25 26 27 28 29 27 28 29 30
30 31
July August September
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 1 2 3 4 5 6 7 1 2 3
3 4 5 6 7 8 9 8 9 10 11 12 13 14 4 5 6 7 8 9 10
10 11 12 13 14 15 16 15 16 17 18 19 20 21 11 12 13 14 15 16 17
17 18 19 20 21 22 23 22 23 24 25 26 27 28 18 19 20 21 22 23 24
24 25 26 27 28 29 30 29 30 31 25 26 27 28 29 30
31
October November December
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 1 2 3 4 5 6 7 1 2 3 4 5
3 4 5 6 7 8 9 8 9 10 11 12 13 14 6 7 8 9 10 11 12
10 11 12 13 14 15 16 15 16 17 18 19 20 21 13 14 15 16 17 18 19
17 18 19 20 21 22 23 22 23 24 25 26 27 28 20 21 22 23 24 25 26
24 25 26 27 28 29 30 29 30 27 28 29 30 31
2022 年是否是闰年: False
2022 年 2 月日历:
February 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
时间格式化
将时间以指定格式输出是常见的操作,下面列举了一些常用的格式化字符串:
%Y:四位数的年份(例如:2022)%m:两位数的月份(01-12)%d:两位数的日期(01-31)%H:24 小时制小时数(00-23)%M:分钟数(00-59)%S:秒数(00-59)
示例代码如下:
import datetime
now = datetime.datetime.now()
formatted_datetime = now.strftime("%Y-%m-%d %H:%M:%S")
print("当前时间:", formatted_datetime)
运行上述代码,输出如下:
当前时间: 2022-02-15 10:30:00
总结
本文介绍了 Python 中处理时间的相关模块和函数,包括 time、datetime 和 calendar。通过这些模块和函数,我们可以方便地获取当前时间、格式化输出时间以及进行时间计算等操作。
极客教程