datetime 模組學習

來源:互聯網
上載者:User

標籤:class   code   imp   spl   圖片   play   print   none   minutes   

 1 # Author: Sure Feng 2  3 ‘‘‘ 4 datetime模組簡單學習 5  6 學會時間加減 7 ‘‘‘ 8  9 import datetime, time10 11 # 擷取目前時間12 n_time = datetime.datetime.now()13 print("目前時間:",n_time) #返回<class ‘datetime.datetime‘>14 15 # 時間戳記直接轉換為日期格式16 n_date = datetime.date.fromtimestamp(time.time())17 print("當前日期:",n_date) #返回<class ‘datetime.date‘>18 19 # 擷取一時間段20 print("================= 一時間段 ======================")21 print(datetime.timedelta(days=1,seconds=2,minutes=3,hours=4,weeks=5)) #返回 36 days, 4:03:0222 print(datetime.timedelta(microseconds=1,milliseconds=2)) #0:00:00.00200123 24 25 # 時間加減26 print("================= 時間加減 ======================")27 p_day = datetime.timedelta(3) # <class ‘datetime.timedelta‘>28 print("目前時間加3天:", n_time + p_day) #目前時間加3天29 p_hour = datetime.timedelta(hours=5)30 print("目前時間前5小時:", n_time + p_hour) #目前時間前5小時31 print("當前日期加3天:", n_date + p_day) #目前時間前5小時32 # 日期和時間段相加,超24h,日期加1天,如此類推33 print("當前日期前5小時:", n_date + p_hour) #目前時間前5小時
module_datetime

 

目前時間: 2018-10-03 15:10:45.273755當前日期: 2018-10-03================= 一時間段 ======================36 days, 4:03:020:00:00.002001================= 時間加減 ======================目前時間加3天: 2018-10-06 15:10:45.273755目前時間前5小時: 2018-10-03 20:10:45.273755當前日期加3天: 2018-10-06當前日期前5小時: 2018-10-03
answer

 

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.