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

python时间格式化

作者:屰兲洏垳&   发布日期:2025-10-03   浏览:45

# Python 时间格式化示例

from datetime import datetime

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

# 格式化时间
formatted_time = now.strftime("%Y-%m-%d %H:%M:%S")
print("格式化后的时间:", formatted_time)

# 解释说明:
# strftime 是 datetime 对象的一个方法,用于将日期和时间格式化为字符串。
# %Y 表示四位数的年份(如 2023)
# %m 表示两位数的月份(01 到 12)
# %d 表示两位数的日期(01 到 31)
# %H 表示两位数的小时(00 到 23)
# %M 表示两位数的分钟(00 到 59)
# %S 表示两位数的秒(00 到 59)

# 另一个例子:只获取年月日
date_only = now.strftime("%Y-%m-%d")
print("只包含日期的格式化时间:", date_only)

上一篇:python批量重命名文件

下一篇:python wordcloud

大家都在看

python时间格式

python读取文件路径

staticmethod在python中有

python开发windows应用程序

python中len是什么意思

python ord和chr

python中的yield

python自定义异常

python判断路径是否存在

python list.pop

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

Laravel 中文站