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

python time模块

作者:傲世,小狂徒   发布日期:2025-05-27   浏览:82

import time

# 获取当前时间的时间戳(自1970年1月1日午夜以来的秒数)
timestamp = time.time()
print(f"当前时间戳: {timestamp}")

# 将时间戳转换为可读格式的时间字符串
readable_time = time.ctime(timestamp)
print(f"可读格式的时间: {readable_time}")

# 暂停程序执行一段时间(秒)
print("暂停5秒...")
time.sleep(5)
print("5秒后继续")

# 获取当前本地时间的struct_time对象
local_time = time.localtime()
print(f"本地时间: {local_time}")

# 格式化时间,将struct_time对象转换为指定格式的字符串
formatted_time = time.strftime("%Y-%m-%d %H:%M:%S", local_time)
print(f"格式化后的时间: {formatted_time}")

# 将时间字符串解析为struct_time对象
time_string = "2023-10-01 12:00:00"
parsed_time = time.strptime(time_string, "%Y-%m-%d %H:%M:%S")
print(f"解析后的时间: {parsed_time}")

解释说明:

  1. time.time():返回当前时间的时间戳,即自1970年1月1日午夜以来的秒数。
  2. time.ctime():将时间戳转换为可读格式的时间字符串。
  3. time.sleep():暂停程序执行指定的秒数。
  4. time.localtime():获取当前本地时间的struct_time对象。
  5. time.strftime():将struct_time对象格式化为指定格式的字符串。
  6. time.strptime():将时间字符串解析为struct_time对象。

这些函数可以帮助你在Python中处理和操作时间。

上一篇:python json格式化

下一篇:python中+=

大家都在看

python时间格式

python ord和chr

python中的yield

python自定义异常

python list.pop

python的for i in range

npm config set python

python代码简单

python读取文件夹

python中turtle

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

Laravel 中文站