python中关于时间time模块的知识
来源:互联网 发布:淘宝的地址怎么填 编辑:程序博客网 时间:2024/05/20 05:24
nowTime=time.localtime()
获取当前系统日期:
nowDate=datetime.datetime(nowTime[0],nowTime[1],nowTime[2])
日期格式转换:
baseTime="20121012" #转换成2012-10-12
mouth = baseTime[4:6]
year = baseTime[0:4]
day = baseTime[6:8]
Destime = year +"-"+mouth+"-"+day
获取当前系统时间,并转换成字符串类型:
curTime = time.strftime("%Y-%m-%d %X", time.localtime(time.time()))
计算给定时间delta天后或delta天前的时间:
baseTime="20121012" #字符串日期
delta=3 #计算3天后的日期,如果是delta=-3,计算3天前的日期
d1 = datetime.datetime(string.atoi(baseTime[0:4]),string.atoi(baseTime[4:6]),string.atoi(baseTime[6:8]))
d2 = d1 + datetime.timedelta(days = delta)
deltaDate=d2.strftime("%Y%m%d")
字符串转换成时间 string -> time 和 time -> string 和 time -> datetime:
date="2012-04-05"
print type(date) #查看date的类型<type 'str'>
date=time.strptime(date,"%Y-%m-%d") #字符串string类型时间转换成time类型时间
print type(date) #查看date的类型<type 'time.struct_time'>
date=time.strftime("%Y-%m-%d",date) #time类型时间转换成字符串string类型时间
rint type(date) #查看date的类型<type 'str'>
date="2012-04-05"
date=time.strptime(date,"%Y-%m-%d") #字符串string类型时间转换成time类型时间
print type(date) #查看date的类型<type 'time.struct_time'>
date=datetime.datetime(date[0],date[1],date[2]) #time类型时间转换成datetime类型时间
print type(date) #查看date的类型<type 'datetime.datetime'>
计算两个日期之间的时间查:
d1 = datetime.datetime(2005, 2, 16)
d2 = datetime.datetime(2004, 12, 31)
# 结果:47
#上例演示了计算两个日期相差天数的计算。
starttime = datetime.datetime.now()
....
endtime = datetime.datetime.now()
print (endtime - starttime).seconds
#上例演示了计算运行时间的例子,以秒进行显示。
d1 = datetime.datetime.now()
d3 = d1 + datetime.timedelta(days =10)
print str(d3)
print d3.ctime()# 上例演示了计算当前时间向后10天的时间。
- python中关于时间time模块的知识
- Python中time和datetime模块对时间的操作
- python的时间处理模块 time
- python的时间处理模块 time
- python常用的时间处理模块time
- 关于Python的time模块常用函数
- Python中time模块
- Python time时间模块学习
- python时间模块time详解
- python时间time模块介绍
- Python时间模块之time
- Python time 模块中sleep的使用说明
- Python时间模块之Time模块解析
- Python 的 time模块
- python的time模块
- PYTHON的TIME模块
- Python的time模块
- python 的 time 模块
- Hadoop-08-Hive本地独立式安装
- 看过的觉得比较好的一些emacs使用资料
- android开发环境搭建win7
- HTML5学习之路(二)-地理定位
- 查找与排序 In Java
- python中关于时间time模块的知识
- 一个客户端配置多个Git账户
- Reveal---iOS上的屠龙刀
- codevs1286 郁闷的出纳员
- 快速排序
- linux达人养成计划I
- win7命令行静默安装SQLServer2008R2脚本
- ServletContext
- PC键盘在Mac下Command/Option键切换