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

python 时间

作者:一杯酒悲痛人   发布日期:2025-10-26   浏览:34

import datetime

# 获取当前时间
current_time = datetime.datetime.now()
print("当前时间:", current_time)

# 获取指定格式的时间字符串
formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S")
print("格式化后的时间:", formatted_time)

# 将字符串转换为时间对象
time_str = "2023-10-01 12:00:00"
time_obj = datetime.datetime.strptime(time_str, "%Y-%m-%d %H:%M:%S")
print("从字符串转换的时间对象:", time_obj)

# 计算两个时间之间的差值
time_diff = datetime.datetime.now() - time_obj
print("时间差:", time_diff)

解释说明:

  1. 获取当前时间:使用 datetime.datetime.now() 获取当前的日期和时间。
  2. 格式化时间:使用 strftime 方法将时间对象转换为指定格式的字符串。
  3. 字符串转时间对象:使用 strptime 方法将时间字符串解析为时间对象。
  4. 计算时间差:通过减法操作计算两个时间对象之间的时间差。

上一篇:reverse=true在python 中

下一篇:python kafka

大家都在看

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 中文站