python基礎學習時間time和datetime模組

來源:互聯網
上載者:User

標籤:format   查看   格林威治   時區   time()   local   timestamp   for   ace   

時間模組time和datetime()
時間表現為三種格式 1.時間戳記 2.時間元組 3.格式化字串(2017-11-1)
時間戳記:通常來說時間戳記表示的是從格林威治1970年1月1日00:00:00開始按秒計算的位移量
時區:世界標準時間 UTC (格林威治天文時間)
中國時間為UTC+8 表示東八區
DST:夏令時


import time

time.time() #擷取當前的時間戳記
time.timezone() #查看當前的本地時間和世界標準時間的差值(秒)
time.daylight() #查看當地時間是否啟動了夏令時
time.sleep() #休息(秒)


時間戳記與時間元組之間的轉換

time.gmtime() #擷取一個當前UTC時間元組,參數為時間戳記,無參數時預設使用當前的時間戳記
time.local() #擷取一個當前的本地時間元祖,參數為時間戳記。無參數時預設使用目前時間戳
time.mktime() #將一個時間元群組轉換為時間戳記

時間元組與格式化字串之間的轉換
time.strftime(format,tuple) #將時間元祖轉換為格式化字串
格式為 %Y年 %m月 %d日 %H時(24時制) %M分鐘 %S秒
例如:time.strftime("%Y-%m-%d %H:%M:%S",tuple) 格式為:2017-11-1 12:48:20
time.strptime(字串,格式) #將格式化字元轉換為時間元組
time.asctime() #將一個時間元祖轉換為:Sun Nov 5 12:53:03 2017格式的字串
time.ctime() #將一個時間戳記轉換為:Sun Nov 5 12:53:03 2017格式的字串


import datetime

datetime.datetime.now() #擷取當前的時間,格式:2017-11-05 12:58:08.998633
datetime.date.fromtimestamp #將時間戳記轉換為格式:2017-11-05

時間加減法
datetime.datetime.now() + datetime.timedelta(3) #將當前日期+3天(負數為-3天)
datetime.datetime.now() + datetime.timedelta(minutes=30) #將目前時間+30分鐘
datetime.datetime.now() + datetime.timedelta(hours=3) #將目前時間+3小時

修改當前的時間
c_time = datetime.datetime.now()
c_time.replace(minute=30,hours=2)

python基礎學習時間time和datetime模組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.