一尸饺、time模塊
time模塊中時間表現(xiàn)的格式主要有三種:
a、timestamp時間戳柿估,時間戳表示的是從1970年1月1日00:00:00開始按秒計算的偏移量
b跳夭、struct_time時間元組宜猜,共有九個元素組泼返。
c、format time 格式化時間姨拥,已格式化的結(jié)構(gòu)使時間更具可讀性绅喉。包括自定義格式和固定格式。
1叫乌、時間格式轉(zhuǎn)換圖:
2:示例
import?time??
#?生成timestamp??
time.time()??
#?1477471508.05??
#struct_time?to?timestamp??
time.mktime(time.localtime())??
#生成struct_time??
#?timestamp?to?struct_time?本地時間??
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?格林威治時間??
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??
#生成固定格式的時間表示格式??
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對象??
d1.year块请、date.month灌诅、date.day:年、月稳诚、日盔憨;??
d1.replace(year,?month,?day):生成一個新的日期對象浙宜,用參數(shù)指定的年平夜,月线脚,日代替原有對象中的屬性。(原有對象仍保持不變)??
d1.timetuple():返回日期對應的time.struct_time對象额各;??
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類所能表示的最小、最大時間蔫饰。其中琅豆,time.min?=?time(0,?0,?0,?0),?time.max?=?time(23,?59,?59,?999999)篓吁;??time.resolution:時間的最小單位茫因,這里是1微秒;?
方法和屬性
t1?=?datetime.time(10,23,15)#time對象??
t1.hour杖剪、t1.minute节腐、t1.second、t1.microsecond:時摘盆、分翼雀、秒、微秒孩擂;??t1.tzinfo:時區(qū)信息狼渊;?
?t1.replace([?hour[?,?minute[?,?second[?,?microsecond[?,?tzinfo]?]?]?]?]?):創(chuàng)建一個新的時間對象,用參數(shù)指定的時类垦、分狈邑、秒、微秒代替原有對象中的屬性(原有對象仍保持不變)蚤认;??
t1.isoformat():返回型如"HH:MM:SS"格式的字符串表示米苹;?
?t1.strftime(fmt):同time模塊中的format;??
3砰琢、datetime類
datetime相當于date和time結(jié)合起來蘸嘶。
datetime.datetime (year, month, day[ , hour[ , minute[ , second[ , microsecond[ , tzinfo] ] ] ] ] )
靜態(tài)方法和字段
datetime類????datetime相當于date和time結(jié)合起來良瞧。?
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對象??
dt.year、month训唱、day褥蚯、hour、minute况增、second赞庶、microsecond、tzinfo:??
dt.date():獲取date對象澳骤;??
dt.time():獲取time對象歧强;??
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?():返回一個日期時間的C格式字符串,等效于time.ctime(time.mktime(dt.timetuple()))为肮;??
dt.?strftime?(format)??
timedelta類誊锭,時間加減
#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?