Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

python格式化时间

作者:续写丶未来式   发布日期:2026-04-20   浏览:11

from datetime import datetime

# 获取当前时间
now = datetime.now()

# 格式化时间为字符串
formatted_time = now.strftime("%Y-%m-%d %H:%M:%S")

print("格式化后的时间:", formatted_time)

解释说明:

  1. 导入模块from datetime import datetime 导入了 Python 的 datetime 模块,用于处理日期和时间。
  2. 获取当前时间now = datetime.now() 获取当前的日期和时间,并将其存储在变量 now 中。
  3. 格式化时间formatted_time = now.strftime("%Y-%m-%d %H:%M:%S") 使用 strftime 方法将 now 转换为指定格式的字符串。格式化字符串 "%Y-%m-%d %H:%M:%S" 表示年-月-日 小时:分钟:秒。
  4. 输出结果print("格式化后的时间:", formatted_time) 打印格式化后的时间字符串。

如果你需要其他格式的时间字符串,可以调整 strftime 方法中的格式化字符串。

上一篇:python保存

下一篇:python 生成excel

大家都在看

python 二维码识别

python excel 库

python时间格式

pythoneval函数用法

列表切片操作python

python读取文件路径

staticmethod在python中有

python 保存json文件

python开发windows应用程序

python中len是什么意思

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站