Python 生成当前时间并格式化输出:2022-10-25 18:30
可以使用 Python 内置的 datetime 模块来生成当前时间,然后使用 strftime 函数将时间格式化为指定的字符串格式。
以下是一个示例代码:
import datetime
now = datetime.datetime.now()
formatted_time = now.strftime('%Y-%m-%d %H:%M')
print(formatted_time)
输出结果为:
2022-10-25 18:30
其中,%Y 表示年份,%m 表示月份,%d 表示日期,%H 表示小时,%M 表示分钟。更多的格式化选项可以参考 Python 官方文档。
原文地址: https://www.cveoy.top/t/topic/jZI2 著作权归作者所有。请勿转载和采集!