date and time python code
AI 回答
由 glm-4-flash 生成 · 仅供参考,请自行核实
1. 获取当前日期和时间:
from datetime import datetime
now = datetime.now()
print(now)
2. 格式化日期和时间:
formatted_now = now.strftime('%Y-%m-%d %H:%M:%S')
print(formatted_now)
3. 将字符串转换为日期和时间对象:
from datetime import datetime
date_string = '2021-12-01 12:00:00'
date_obj = datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S')
print(date_obj)
from datetime import datetime
now = datetime.now()
print(now)
2. 格式化日期和时间:
formatted_now = now.strftime('%Y-%m-%d %H:%M:%S')
print(formatted_now)
3. 将字符串转换为日期和时间对象:
from datetime import datetime
date_string = '2021-12-01 12:00:00'
date_obj = datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S')
print(date_obj)
没有找到「date and time python code」相关的内容
建议:检查关键词是否有错别字,或换一个更常见的说法。