获取当前日期和时间是 Python 编程中的一项常见任务。有多种方法可以做到这一点,具体取决于您的需求和可用的库。在本文中,我们将探讨开发人员在 Python 中获取当前日期和时间的一些最常用方法,包括使用内置的 datetime 模块。
要了解更多信息,请访问我们的深入的Python日期时间教程。
在 Python 中获取当前日期和时间
要获取当前日期和时间,您可以使用`datetime.now()`
函数,它返回一个日期时间对象,表示本地时区的当前日期和时间。
|
import datetime
当前日期时间 = datetime.datetime.now()
print(当前日期时间)
|
上面的脚本将输出以下内容:
2022-12-25 18:13:13.363653
要以特定方式格式化日期和时间,您可以使用`strftime()`
datetime 对象的方法,它采用格式字符串作为参数。格式字符串指定日期和时间值的格式。
例如,要获取当前日期作为字符串`YYYY-MM-DD HH:MM:SS`
格式,您可以使用`%Y-%m-%d %H:%M:%S`
格式字符串:
|
import datetime
当前日期时间 = datetime.datetime.now()
当前日期 = 当前日期时间.strftime(“%Y-%m-%d %H:%M:%S”)
print(当前日期)
|
上面的脚本将输出以下内容:
2022-12-25 18:15:03
Python `datetime`
模块类属性
Python 中的 datetime 模块提供了用于处理日期、时间和时间戳的类。主要类是 datetime,它表示单个时间点。
以下是 datetime 类的一些常见属性:
-
`year`
:年份(四位数)
-
`month`
:月份(1-12)
-
`day`
:该月的第几天 (1-31)
-
`hour`
:一天中的小时 (0-23)
-
`minute`
:一小时中的分钟 (0-59)
-
`second`
:分钟的第二个(0-59)
-
`microsecond`
:秒的微秒(0-999999)
-
`tzinfo`
:代表时区的对象
以下是创建日期时间对象并访问其属性的示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
from datetime import datetime
dt = datetime(2022, 12, 31, 23, 59, 59)
## 取消下面的注释以获取当前日期和时间
#dt = 日期时间.now()
dt = datetime.now()
print(dt.year) # 打印 2022
print(dt.month) # 打印 12
print(dt.day) # 打印 31
print(dt.hour) # 打印 23
print(dt.minute) # 打印 59
print(dt.second) # 打印 59
print(dt.微秒) # 打印 0
print(dt.tzinfo) # 打印无
|
在 Python 中获取格式化的日期时间
要在 Python 中获取格式化的日期和时间字符串,您可以使用`strftime`
的方法`datetime`
class.
这是一个使用的例子`strftime`
获取格式化的日期和时间字符串:
|
from datetime import datetime
# 创建一个日期时间对象
dt = datetime(2022, 12, 31, 23, 59, 59)
## 取消下面的注释以获取当前日期和时间
#dt = 日期时间.now()
# 使用strftime创建格式化字符串
格式化日期 = dt.strftime(“%Y-%m-%d %H:%M:%S”)
print(格式化日期) # 打印“2022-12-31 23:59:59”
|
The `strftime`
方法采用格式字符串作为参数,该参数指定如何格式化日期和时间。在上面的示例中,格式字符串“%Y-%m-%d %H:%M:%S”指定年份应格式化为四位数字 (`%Y`
),月份为两位数 (`%m`
),日期为两位数 (`%d`
),小时为两位数 (`%H`
),分钟为两位数 (`%M`
),第二个为两位数(`%S`
).
您可以使用不同的格式代码来自定义输出`strftime`
根据您的喜好。例如,您可以使用`%A`
获取星期几的全名,`%B`
获取月份的全名,以及`%I`
获取 12 小时格式的小时,并带有前导零(表示单位数小时)。
以下是 strftime 格式代码的更多示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
from datetime import datetime
# 创建一个日期时间对象
dt = datetime(2022, 12, 31, 23, 59, 59)
## 取消下面的注释以获取当前日期和时间
#dt = 日期时间.now()
# 使用strftime创建格式化字符串
print(dt.strftime(“%Y-%m-%d %H:%M:%S”)) # 打印 2022-12-31 23:59:59
print(dt.strftime(“%Y/%m/%d”)) # 打印 2022/12/31
print(dt.strftime(“%H:%M:%S”)) # 打印 23:59:59
print(dt.strftime(“%I:%M:%S%p”)) # 打印 11:59:59 PM
print(dt.strftime(“%a,%b %d,%Y”)) # 打印 2022 年 12 月 31 日星期六
print(dt.strftime(“%a,%b %d,%Y %H:%M:%S”)) # 打印 2022 年 12 月 31 日星期六 23:59:59
print(dt.strftime(“%a %b %d %H:%M:%S %Y”)) # 打印 2022 年 12 月 31 日星期六 23:59:59
print(dt.strftime(“%A,%B%d,%Y%I:%M%p”)) # 打印 2022 年 12 月 31 日星期六晚上 11:59"
|
下面是可用于格式化 Python 脚本中的日期和时间输出的指令列表。
Directive |
Meaning |
%a |
Locale’s abbreviated weekday name. |
%A |
Locale’s full weekday name. |
%b |
Locale’s abbreviated month name. |
%B |
Locale’s full month name. |
%c |
Locale’s appropriate date and time representation. |
%d |
Day of the month as a decimal number [01,31]. |
%H |
Hour (24-hour clock) as a decimal number [00,23]. |
%I |
Hour (12-hour clock) as a decimal number [01,12]. |
%j |
Day of the year as a decimal number [001,366]. |
%m |
Month as a decimal number [01,12]. |
%M |
Minute as a decimal number [00,59]. |
%p |
Locale’s equivalent of either AM or PM. |
%S |
Second as a decimal number [00,61]. |
%U |
Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0. |
%w |
Weekday as a decimal number [0(Sunday),6]. |
%W |
Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0. |
%x |
Locale’s appropriate date representation. |
%X |
Locale’s appropriate time representation. |
%y |
Year without century as a decimal number [00,99]. |
%Y |
Year with century as a decimal number. |
%z |
Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59]. |
%Z |
Time zone name (no characters if no time zone exists). |
%% |
A literal '%' character. |
我希望这些示例能够帮助您了解如何在 Python 中获取当前日期和时间,以及如何在时区之间进行转换。
结论
总之,datetime 模块是一个方便而强大的工具,用于在 Python 中处理日期、时间和时间戳。您可以使用 datetime.now() 函数获取本地时区的当前日期和时间,或使用 datetime.utcnow() 函数获取 UTC 时区的当前日期和时间。您还可以使用 datetime() 构造函数为特定日期和时间创建日期时间对象。