如何在 Python 中获取当前位置的日期和时间

How to get the Date and Time of Current Location in Python

我想获取当前位置的日期和时间。如果有人在不同的国家运行代码,认为它会为他们显示日期和时间,而不是为我显示。如果您知道这方面的任何命令,请在下方留言。我更喜欢If I don't need a library这样拿到代码的人就不用下载library了

我想你无法获取位置的日期或时间。

此代码获取您设备的日期和时间:

from datetime import datetime
now = datetime.now()

current_time = now.strftime("%H:%M:%S")
current_date = now.strftime("%D")

print("Current Time: ", current_time)
print("Current Date: ", current_date)

一个名为 datetime 的标准 Python 模块可能可以帮助您从程序所在位置检索当前系统日期和时间 运行 由于它是一个标准模块,因此无需安装它,只需将其导入程序本身即可。

示例:

import datetime
x = datetime.datetime.now()
print(x)

会给你输出:

2019-07-20 00:52:27.158746

以上输出包含当前年月日时分秒微秒。