python-day21--time模組

來源:互聯網
上載者:User

標籤:倫敦   time   之間   div   類型   計算   format   time()   play   

一、三種表示方法

1.時間戳記(timestamp):

time.time( )       #得到的是float類型

2.格式化(Format String):

time.strftime(‘%Y/%m/%d %H:%M:%S‘)       #得到的是字串類型

3.結構化(struct_time):

time.localtime( )

#匯入時間模組>>>import time#時間戳記>>>time.time()1500875844.800804#時間字串>>>time.strftime("%Y-%m-%d %X")‘2017-07-24 13:54:37‘>>>time.strftime("%Y-%m-%d %H-%M-%S")‘2017-07-24 13-55-04‘#時間元組:localtime將一個時間戳記轉換為當前時區的struct_timetime.localtime()time.struct_time(tm_year=2017, tm_mon=7, tm_mday=24,          tm_hour=13, tm_min=59, tm_sec=37,                  tm_wday=0, tm_yday=205, tm_isdst=0)

小結:時間戳記是電腦能夠識別的時間;時間字串是人能夠看懂的時間;元組則是用來操作時間的

二、幾種格式之間的轉換

#時間戳記-->結構化時間#time.gmtime(時間戳記)    #UTC時間,與英國倫敦當地時間一致#time.localtime(時間戳記) #當地時間。例如我們現在在北京執行這個方法:與UTC時間相差8小時,UTC時間+8小時 = 北京時間 >>>time.gmtime(1500000000)time.struct_time(tm_year=2017, tm_mon=7, tm_mday=14, tm_hour=2, tm_min=40, tm_sec=0, tm_wday=4, tm_yday=195, tm_isdst=0)>>>time.localtime(1500000000)time.struct_time(tm_year=2017, tm_mon=7, tm_mday=14, tm_hour=10, tm_min=40, tm_sec=0, tm_wday=4, tm_yday=195, tm_isdst=0)#結構化時間-->時間戳記 #time.mktime(結構化時間)>>>time_tuple = time.localtime(1500000000)>>>time.mktime(time_tuple)1500000000.0
View Code
#結構化時間-->字串時間#time.strftime("格式定義","結構化時間")  結構化時間參數若不傳,則現實目前時間>>>time.strftime("%Y-%m-%d %X")‘2017-07-24 14:55:36‘>>>time.strftime("%Y-%m-%d",time.localtime(1500000000))‘2017-07-14‘#字串時間-->結構化時間#time.strptime(時間字串,字串對應格式)>>>time.strptime("2017-03-16","%Y-%m-%d")time.struct_time(tm_year=2017, tm_mon=3, tm_mday=16, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=75, tm_isdst=-1)>>>time.strptime("07/24/2017","%m/%d/%Y")time.struct_time(tm_year=2017, tm_mon=7, tm_mday=24, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=205, tm_isdst=-1)
View Code

#結構化時間 --> %a %b %d %H:%M:%S %Y串#time.asctime(結構化時間) 如果不傳參數,直接返回目前時間的格式化串>>>time.asctime(time.localtime(1500000000))‘Fri Jul 14 10:40:00 2017‘>>>time.asctime()‘Mon Jul 24 15:18:33 2017‘#%a %d %d %H:%M:%S %Y串 --> 結構化時間#time.ctime(時間戳記)  如果不傳參數,直接返回目前時間的格式化串>>>time.ctime()‘Mon Jul 24 15:19:07 2017‘>>>time.ctime(1500000000)‘Fri Jul 14 10:40:00 2017‘
View Code

 

python-day21--time模組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.