Python time datetime 模块梳理

time是Python底层的模块,datetime是基于time的实现

time相关的数据类型 float ,struct_time ,string 

函数有 以下

<span style="font-size:18px;">>>> import time
>>> print time.time()
1413715747.11
>>> print type(time.time()).__name__
float
>>> print time.localtime()
time.struct_time(tm_year=2014, tm_mon=10, tm_mday=19, tm_hour=18, tm_min=49, tm_sec=34, tm_wday=6, tm_yday=292, tm_isdst=0)
>>> print type(time.localtime()).__name__
struct_time
>>> print time.mktime(time.localtime())
1413715806.0
>>> time.strptime("2009-06-23 15:00:00","%Y-%m-%d %H:%M:%S")
time.struct_time(tm_year=2009, tm_mon=6, tm_mday=23, tm_hour=15, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=174, tm_isdst=-1)
>>> print time.strftime("%Y",time.gmtime())
2014</span>
mktime(struct_time)  struct_time=>float

strftime(format,struct_time)  struct_time=>string

strptime(string,struct_time) string=>struct_time

localtime(float) float=>struct_time

清晰多了吧

datetime相关的数据类型 datetime,string ,struct_time

>>> a=datetime.datetime(2012,9,3,21,30)
>>> print a.year
2012
>>> date_str = "2008-11-10 17:53:59"
>>> datetime.datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S")
datetime.datetime(2008, 11, 10, 17, 53, 59)
>>> print datetime.datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S")
2008-11-10 17:53:59
>>> a=datetime.datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S")
>>> print type(a).__name__
datetime
>>> print a.timetuple()
time.struct_time(tm_year=2008, tm_mon=11, tm_mday=10, tm_hour=17, tm_min=53, tm_sec=59, tm_wday=0, tm_yday=315, tm_isdst=-1)
>>> format = "output-%Y-%m-%d-%H%M%S.txt" 
>>> print a.strftime(format)
output-2008-11-10-175359.txt
datetime.timetuple()  datetime=>struct_time

datetime.strftime(format) datetime=>string

datetime.strptime(string,format) string=>datetime

datetime.fromtimestamp(float) float=>datetime

datetime的计算

datetime.datetime.now()-datetime.timedelta(minutes=1)

返回上一分钟


然后互相转换也十分的明了了

参考:http://www.tutorialspoint.com/python/python_date_time.htm

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值