Products
GG网络技术分享 2025-11-14 07:14 1
根据您给的文档内容,
导入datetime模块:
python
import datetime

用datetime.datetime.now方法获取当前日期和时候:
python
current_time = datetime.datetime.now
Ru果需要获取当前日期,Neng只获取datetime对象中的日期有些:
python
current_date = current_time.date
Ru果需要格式化日期和时候,Neng用strftime方法:
python
formatted_date_time = current_time.strftime
下面是一个完整的示例代码,展示怎么获取并打印当前日期和时候:
python import datetime
current_time = datetime.datetime.now
print)
print)
这段代码会输出类似以下内容:
当前日期和时候是: 2023-04-01 12:34:56
当前日期是: 2023-04-01
当前时候是: 12:34:56
这样,您就Neng轻巧松地在Python中获取并处理当前日期和时候了。
Demand feedback