一、time模塊
time模塊中時(shí)間表現(xiàn)的格式主要有三種:
a、timestamp時(shí)間戳,時(shí)間戳表示的是從1970年1月1日00:00:00開(kāi)始按秒計(jì)算的偏移量
b、struct_time時(shí)間元組,共有九個(gè)元素組。
c、format time 格式化時(shí)間,已格式化的結(jié)構(gòu)使時(shí)間更具可讀性。包括自定義格式和固定格式。
1、時(shí)間格式轉(zhuǎn)換圖:
2:示例
import?time??
#?生成timestamp??
time.time()??
#?1477471508.05??
#struct_time?to?timestamp??
time.mktime(time.localtime())??
#生成struct_time??
#?timestamp?to?struct_time?本地時(shí)間??
time.localtime()??
time.localtime(time.time())??
#?time.struct_time(tm_year=2016,?tm_mon=10,?tm_mday=26,?tm_hour=16,?tm_min=45,?tm_sec=8,?tm_wday=2,?tm_yday=300,?tm_isdst=0)??
#?timestamp?to?struct_time?格林威治時(shí)間??
time.gmtime()??
time.gmtime(time.time())??
#?time.struct_time(tm_year=2016,?tm_mon=10,?tm_mday=26,?tm_hour=8,?tm_min=45,?tm_sec=8,?tm_wday=2,?tm_yday=300,?tm_isdst=0)??
#format_time?to?struct_time? ·
time.strptime('2011-05-05?16:37:06',?'%Y-%m-%d?%X')??
#?time.struct_time(tm_year=2011,?tm_mon=5,?tm_mday=5,?tm_hour=16,?tm_min=37,?tm_sec=6,?tm_wday=3,?tm_yday=125,?tm_isdst=-1)??
#生成format_time??
#struct_time?to?format_time??
time.strftime("%Y-%m-%d?%X")??
time.strftime("%Y-%m-%d?%X",time.localtime())??
#?2016-10-26?16:48:41??
#生成固定格式的時(shí)間表示格式??
time.asctime(time.localtime())??
time.ctime(time.time())??
#?Wed?Oct?26?16:45:08?2016?
datetime模塊
1:date類
靜態(tài)方法和字段
from?datetime?import?*?
import?time????
print???'date.max:',?date.max?
?print???'date.min:',?date.min??
print???'date.today():',?date.today()??
print???'date.fromtimestamp():',?date.fromtimestamp(time.time())????
#Output======================??#?
date.max:?9999-12-31??
date.min:?0001-01-01??
?date.today():?2016-10-26??
date.fromtimestamp():?2016-10-26????output??
方法和屬性
d1?=?date(2011,06,03)#date對(duì)象??
d1.year、date.month、date.day:年、月、日;??
d1.replace(year,?month,?day):生成一個(gè)新的日期對(duì)象,用參數(shù)指定的年,月,日代替原有對(duì)象中的屬性。(原有對(duì)象仍保持不變)??
d1.timetuple():返回日期對(duì)應(yīng)的time.struct_time對(duì)象;??
d1.weekday():返回weekday,如果是星期一,返回0;如果是星期2,返回1,以此類推;?
?d1.isoweekday():返回weekday,如果是星期一,返回1;如果是星期2,返回2,以此類推;??
d1.isocalendar():返回格式如(year,month,day)的元組;??
d1.isoformat():返回格式如'YYYY-MM-DD’的字符串;??
d1.strftime(fmt):和time模塊format相同。??
2、time類
datetime.time(hour[?, minute[?, second[?, microsecond[?, tzinfo]]]]?)?
靜態(tài)方法和字段
time.min、time.max:time類所能表示的最小、最大時(shí)間。其中,time.min?=?time(0,?0,?0,?0),?time.max?=?time(23,?59,?59,?999999);??time.resolution:時(shí)間的最小單位,這里是1微秒;?
方法和屬性
t1?=?datetime.time(10,23,15)#time對(duì)象??
t1.hour、t1.minute、t1.second、t1.microsecond:時(shí)、分、秒、微秒;??t1.tzinfo:時(shí)區(qū)信息;?
?t1.replace([?hour[?,?minute[?,?second[?,?microsecond[?,?tzinfo]?]?]?]?]?):創(chuàng)建一個(gè)新的時(shí)間對(duì)象,用參數(shù)指定的時(shí)、分、秒、微秒代替原有對(duì)象中的屬性(原有對(duì)象仍保持不變);??
t1.isoformat():返回型如"HH:MM:SS"格式的字符串表示;?
?t1.strftime(fmt):同time模塊中的format;??
3、datetime類
datetime相當(dāng)于date和time結(jié)合起來(lái)。
datetime.datetime (year, month, day[ , hour[ , minute[ , second[ , microsecond[ , tzinfo] ] ] ] ] )
靜態(tài)方法和字段
datetime類????datetime相當(dāng)于date和time結(jié)合起來(lái)。?
datetime.datetime?(year,?month,?day[?,?hour[?,?minute[?,?second[?,?microsecond[?,?tzinfo]?]?]?]?]?)????靜態(tài)方法和字段??
from??datetime?import?*?
?import?time????
print???'datetime.max:',?datetime.max?
print???'datetime.min:',?datetime.min?
?print???'datetime.resolution:',?datetime.resolution??
print???'today():',?datetime.today()??
print???'now():',?datetime.now()??
print???'utcnow():',?datetime.utcnow()??
print???'fromtimestamp(tmstmp):',?datetime.fromtimestamp(time.time())??
print???'utcfromtimestamp(tmstmp):',?datetime.utcfromtimestamp(time.time())????
#output======================??
#?datetime.max:?9999-12-31?23:59:59.999999?
?#?datetime.min:?0001-01-01?00:00:00?
?#?datetime.resolution:?0:00:00.000001?
?#?today():?2016-10-26?23:12:51.307000?
?#?now():?2016-10-26?23:12:51.307000??
#?utcnow():?2016-10-26?15:12:51.307000??
#?fromtimestamp(tmstmp):?2016-10-26?23:12:51.307000??
#?utcfromtimestamp(tmstmp):?2016-10-26?15:12:51.307000? ?
方法和屬性
dt=datetime.now()#datetime對(duì)象??
dt.year、month、day、hour、minute、second、microsecond、tzinfo:??
dt.date():獲取date對(duì)象;??
dt.time():獲取time對(duì)象;??
dt.?replace?([?year[?,?month[?,?day[?,?hour[?,?minute[?,?second[?,?microsecond[?,?tzinfo]?]?]?]?]?]?]?]):??
dt.?timetuple?()?
dt.?utctimetuple?()??
dt.?toordinal?()??
dt.?weekday?()??
dt.?isocalendar?()??
dt.?isoformat?([?sep]?)??
dt.?ctime?():返回一個(gè)日期時(shí)間的C格式字符串,等效于time.ctime(time.mktime(dt.timetuple()));??
dt.?strftime?(format)??
timedelta類,時(shí)間加減
#coding:utf-8??from??datetime?import?*????
dt?=?datetime.now()??#日期減一天??
dt1?=?dt?+?timedelta(days=-1)#昨天??
dt2?=?dt?-?timedelta(days=1)#昨天??
dt3?=?dt?+?timedelta(days=1)#明天??
delta_obj?=?dt3-dt??print?type(delta_obj),
delta_obj#?1?day,?0:00:00??print?delta_obj.days?,delta_obj.total_seconds()#1?86400.0?