使用 Python Datetime.now() 获取今天的日期和时间
你可以使用 Python 中的 datetime
模块来检索有关日期和时间的数据。
在本文中,你将学习如何使用 datetime
模块中的 datetime
对象来获取当前日期和时间属性。
你还将学习如何使用 datetime.now()
函数和 pytz
模块获取世界各地不同地点的日期和时间。
如何在 Python 中使用 datetime 对象
为了使用 datetime
对象,你必须首先导入它,就是这样:
from datetime import datetime
在下一个示例中,你将看到如何使用 datetime
对象。
from datetime import datetime
current_dateTime = datetime.now()
print(current_dateTime)
# 2022-09-20 10:27:21.240752
在上面的代码中,我们将 datetime
分配给了一个名为 current_dateTime
的变量。
当打印到控制台时,我们得到了当前的年、月、日和时间:2022-09-19 17:44:17.858167
。
请注意,我们可以使用 now()
方法访问上述信息:datetime.now()
。
如何使用 datetime.now() 属性
在上一节中,我们检索了有关当前日期和时间的信息,包括当前的年、月、日和时间。
但是 datetime.now()
函数为我们提供了用于提取单个数据的额外属性。
例如,要获取当前年份,你可以执行以下操作:
from datetime import datetime
current_dateTime = datetime.now()
print(current_dateTime.year)
# 2022
在上面的示例中,我们将 datetime.now()
函数分配给了一个名为 current_dateTime
的变量。
使用点表示法,我们将 year
属性附加到上面声明的变量:current_dateTime.year
。当打印到控制台时,我们得到了 2022。
datetime.now()
函数具有以下属性:
year
month
day
hour
minute
second
microsecond
这是上面列出的属性的一个使用示例:
from datetime import datetime
current_dateTime = datetime.now()
print(current_dateTime.year) # 2022
print(current_dateTime.month) # 9
print(current_dateTime.day) # 20
print(current_dateTime.hour) # 11
print(current_dateTime.minute) # 27
print(current_dateTime.second) # 46
print(current_dateTime.microsecond) # 582035
如何在 Python 中使用 datetime.now() 和 pytz 获取特定时区
要在 Python 中获取有关世界各地不同时区的信息,你可以使用 datetime.now()
函数和 pytz
模块。
这是一个示例,显示如何获取尼日利亚拉各斯的当前日期和时间:
from datetime import datetime
import pytz
datetime_in_Lagos = datetime.now(pytz.timezone('Africa/Lagos'))
print(datetime_in_Lagos)
# 2022-09-20 12:53:27.225570+01:00
在上面的代码中,我们首先导入了模块:
from datetime import datetime
import pytz
然后我们将 pytz
对象作为参数传递给 datetime.now()
函数:
datetime_in_Lagos = datetime.now(pytz.timezone('Africa/Lagos'))
pytz
对象有一个 timezone
属性,它获取你正在寻找的特定时区的信息/参数:pytz.timezone('Africa/Lagos')
。
使用 all_timezones
属性,你可以获得 pytz
库中所有可能的时区的列表,这些时区可以作为参数传递给 timezone
属性——就像我们在上一个示例中所做的那样:
from datetime import datetime
import pytz
all_timezones = pytz.all_timezones
print(all_timezones)
# [List of all timezones...]
小结
在本文中,我们讨论了使用 Python 中的 datetime.now()
函数获取今天的日期和时间。
我们看到了一些示例,展示了如何使用 datetime.now()
函数及其属性。
最后,我们看到了如何使用 datetime.now()
函数和 pytz
模块获取世界各地特定位置的日期和时间。
祝你编程愉快!