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