time模块、datetime模块讲解
2024-08-30 09:35:10
time模块清楚三种格式的时间相互转换 import time
# 时间分为三种格式
#1、时间戳
start= time.time()
time.sleep(3)
stop= time.time()
print(stop - start) #2、格式化的字符串形式
print(time.strftime('%Y-%m-%d %X'))
print(time.strftime('%Y-%m-%d %H:%M:%S %p')) #3、 结构化的时间/时间对象
t1=time.localtime()
print(t1)
print(type(t1.tm_min))
print(t1.tm_mday) #通过对象里的属性可以直接进行取值 t2=time.gmtime() # 和localtime一样,都是时间对象,可以通过变量直接取值
print(t1)
print(t2)
2.时间转换
时间转换
print(time.localtime(123123123)) # 转换为格式化对象
print(time.gmtime(123123123)) print(time.mktime(time.localtime())) #格式化对象转化为时间戳 print(time.strftime('%Y',time.localtime()))
print(time.strptime('2011-03-07','%Y-%d-%m'))
print(time.asctime())
print(time.ctime())
print(time.strftime('%a %b %d %H:%M:%S %Y')) print(time.asctime(time.localtime()))
print(time.ctime(123123123)) print(time.strftime('%Y-%m-%d %X')) datetime 模块
# 获取格式化字符串形式的时间麻烦
# 时间戳与格式化时间之间的转换麻烦
# 获取之前或者未来的时间麻烦
import datetime print(datetime.datetime.now())
print(datetime.datetime.fromtimestamp(1231233213)) print(datetime.datetime.now() + datetime.timedelta(days=3))
print(datetime.datetime.now() + datetime.timedelta(days=-3)) s=datetime.datetime.now()
print(s.replace(year=2020))
最新文章
- 【工匠大道】markdown使用技巧
- Postgre cannot insert multiple commands into a prepared statement
- ThroughRain第二次冲刺总结
- 经典SQL语句大全_主外键_约束
- js bom中浏览器兼容问题判断代码
- Entity Framework Code First ---EF Power Tool 和MySql一起使用遇到的问题
- Android事件机制
- IDEA上传项目至git
- postgresql搭建从库
- 解决Configuration 'compile' is obsolete and has been replaced with implementation
- SQLServer数据库文件由高版本向低版本转换
- 从零基础到拿到网易Java实习offer,谈谈我的学习经验
- 【转】AJAX中JSON数据的返回处理问题
- Tests for Variances
- react-router v4.0 知识点
- [视频]K8飞刀 ms15022 office漏洞演示动画
- 【转】Mapped Statements collection does not contain value for解决
- Spring和Mybatis整合过程中遇到的一个找不到sqlSessionFactory或sqlSessionTemplate的异常
- myeclipse 10 +Axis2 1.62 开发WebService手记
- 《Android源码设计模式》--策略模式