python-time模块使用

python-time模块使用

1,两个时间概念
  1. struct_time: time模块的时间对象
  2. 时间戳:从1970年1月1日到现在的秒数,是一个正的浮点数。
2, 日常使用-获取当前时间并将时间按照自己的格式生成字符串

%字符代表变量

  1. %Y 年变量
  2. %m 月变量
  3. %d 日变量
  4. % H 小时变量
  5. %M 分钟变量
  6. %S 秒数变量
python 复制代码
import time

# 1. 获取当前时间, time.localtime()方法返回一个struct_time对象
current = time.localtime()

# 2, 将struct_time对象按照特定格式转换为字符串
# 使用方法:利用time.strftime(字符串格式,struct_time)
current_str = time.strftime('%Y年%m月%d日 %H时%M分%S秒', current)
print(current_str)
'''
2024年01月06日 21时33分05秒
'''
相关推荐
Warson_L16 小时前
Python `Annotated` 与 LangGraph Reducer 学习笔记
python
韩师傅16 小时前
海天线算法的前世今生
python·计算机视觉
韩师傅16 小时前
当你的甲方设备过烂,要如何快速出效果?
python·计算机视觉
Warson_L17 小时前
LangGraph的MessageState and HumanMessage
python
韩师傅17 小时前
当你的甲方吐槽天空不够蓝,你应该如何应对
python·计算机视觉
Warson_L18 小时前
python的类&继承
python
Warson_L18 小时前
类型标注/type annotation
python
ThreeS20 小时前
手搓MiniVLA全实战教程-一步一步用pytorch解释原理与思路
人工智能·python
金銀銅鐵21 小时前
[Python] 模 n 乘法的逆元计算器
python·数学·游戏