Python時間time模組介紹

來源:互聯網
上載者:User

標籤:格式轉換   ctime   ftime   mkt   nbsp   cal   bin   時間戳記   images   

一、明確時間元組
二、測試代碼
#!/usr/bin/env python# -- coding: utf-8 --‘"""時間模組,time的相關操作與測試"""# python中time模組使用例子import time# 1493368879.41,時間戳記print time.time()# 擷取的是一個元組,time.struct_time(tm_year=2017, tm_mon=4, tm_mday=28,# tm_hour=16, tm_min=41, tm_sec=19, tm_wday=4, tm_yday=118, tm_isdst=0)print time.localtime()print time.localtime(time.time())#格式化時間# Fri Apr 28 16:31:38 2017print time.asctime(time.localtime(time.time()))# 時間格式轉換,參數(格式,元組)print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())print time.strftime("%y/%m/%d %H/%M/%S", time.localtime())print time.strftime("%Y--%m--%d %H--%M--%S", time.localtime())print time.strftime("%Y %m %d %H--%M--%S", time.localtime())# 格式化成Sat Mar 28 22:24:24 2016形式print time.strftime("%a %b %d %H:%M:%S %Y", time.localtime())# 字串轉換成元組strtime ="2017/08/23"#元群組類型print time.strptime(strtime,"%Y/%m/%d")# 元群組轉換成時間戳記print time.mktime(time.strptime(strtime,"%Y/%m/%d"))#----------------------------------------------------------------------------------------------
三、python中時間日期格式化符號:
  • %y 兩位元的年份表示(00-99)
  • %Y 四位元的年份表示(000-9999)
  • %m 月份(01-12)
  • %d 月內中的一天(0-31)
  • %H 24小時制小時數(0-23)
  • %I 12小時制小時數(01-12)
  • %M 分鐘數(00=59)
  • %S 秒(00-59)
  • %a 本地簡化星期名稱
  • %A 本地完整星期名稱
  • %b 本地簡化的月份名稱
  • %B 本地完整的月份名稱
  • %c 本地相應的日期表示和時間表示
  • %j 年內的一天(001-366)
  • %p 本地A.M.或P.M.的等價符
  • %U 一年中的星期數(00-53)星期天為星期的開始
  • %w 星期(0-6),星期天為星期的開始
  • %W 一年中的星期數(00-53)星期一為星期的開始
  • %x 本地相應的日期表示
  • %X 本地相應的時間表示
  • %Z 當前時區的名稱
  • %% %號本

四、測試代碼結果

 

  

Python時間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.