Python模組datetime__Python

來源:互聯網
上載者:User

http://docs.python.org/2/library/datetime.html

Python提供了多個內建模組用於操作日期時間,像calendar,time,datetime。time模組我在之前的文章已經有所介紹,它提供的介面與C標準庫time.h基本一致。相比於time模組,datetime模組的介面則更直觀、更容易調用。今天就來講講datetime模組。

    datetime模組定義了兩個常量:datetime.MINYEAR和datetime.MAXYEAR,分別表示datetime所能表示的最小、最大年份。其中,MINYEAR = 1,MAXYEAR = 9999。(對於偶等玩家,這個範圍已經足夠用矣~~)

    datetime模組定義了下面這幾個類: datetime.date:表示日期的類。常用的屬性有year, month, day; datetime.time:表示時間的類。常用的屬性有hour, minute, second, microsecond; datetime.datetime:表示日期時間。 datetime.timedelta:表示時間間隔,即兩個時間點之間的長度。 datetime.tzinfo:與時區有關的相關資訊。(這裡不詳細充分討論該類,感興趣的童鞋可以參考python手冊)

    注:上面這些類型的對象都是不可變(immutable)的。

    下面詳細介紹這些類的使用方式。 date類

    date類表示一個日期。日期由年、月、日組成(地球人都知道~~)。date類的建構函式如下:

    class datetime.date(year, month, day):參數的意義就不多作解釋了,只是有幾點要注意一下: year的範圍是[MINYEAR, MAXYEAR],即[1, 9999]; month的範圍是[1, 12]。(月份是從1開始的,不是從0開始的~_~); day的最大值根據給定的year, month參數來決定。例如閏年2月份有29天;

    date類定義了一些常用的類方法與類屬性,方便我們操作: date.max、date.min:date對象所能表示的最大、最小日期; date.resolution:date對象表示日期的最小單位。這裡是天。 date.today():返回一個表示當前本地日期的date對象; date.fromtimestamp(timestamp):根據給定的時間戮,返回一個date對象; datetime.fromordinal(ordinal):將Gregorian日曆時間轉換為date對象;(Gregorian Calendar:一種日曆表示方法,類似於我國的農曆,西方國家使用比較多,此處不詳細展開討論。)

    使用例子: [python]  view plain copy 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())      # # ---- 結果 ----   # date.max: 9999-12-31   # date.min: 0001-01-01   # date.today(): 2010-04-06   # date.fromtimestamp(): 2010-04-06  

    date提供的執行個體方法和屬性: date.year、date.month、date.day:年、月、日; date.replace(year, month, day):產生一個新的日期對象,用參數指定的年,月,日代替原有對象中的屬性。(原有對象仍保持不變) date.timetuple():返回日期對應的time.struct_time對象; date.toordinal():返回日期對應的Gregorian Calendar日期; date.weekday():返回weekday,如果是星期一,返回0;如果是星期2,返回1,以此類推; data.isoweekday():返回weekday,如果是星期一,返回1;如果是星期2,返回2,以此類推; date.isocalendar():返回格式如(year,month,day)的元組; date.isoformat():返回格式如'YYYY-MM-DD’的字串; date.strftime(fmt):自訂格式化字串。在下面詳細講解。

    使用例子: [python]  view plain copy now = date(2010, 04, 06)   tomorrow = now.replace(day = 07)   print 'now:', now, ', tomorrow:', tomorrow   print 'timetuple():', now.timetuple()   print 'weekday():', now.weekday()   print 'isoweekday():', now.isoweekday()   print 'isocalendar():', now.isocalendar()   print 'isoformat():', now.isoformat()      # # ---- 結果 ----   # now: 2010-04-06 , tomorrow: 2010-04-07   # timetuple(): (2010, 4, 6, 0, 0, 0, 1, 96, -1)   # weekday(): 1   # isoweekday(): 2   # isocalendar(): (2010, 14, 2)   # isoformat(): 2010-04-06  

    date還對某些操作進行了重載,它允許我們對日期進行如下一些操作: date2 = date1 + timedelta  # 日期加上一個間隔,返回一個新的日期對象(timedelta將在下面介紹,表示時間間隔) date2 = date1 - timedelta   # 日期隔去間隔,返回一個新的日期對象 timedelta = date1 - date2   # 兩個日期相減,返回一個時間間隔對象 date1 < date2  # 兩個日期進行比較

    註:對日期進行操作時,要防止日期超出它所能表示的範圍。

    使用例子: [python]  view plain copy now = date.today()   tomorrow = now.replace(day = 7)   delta = tomorrow - now   print 'now:', now, ' tomorrow:', tomorrow   print 'timedelta:', delta   print now + delta  

聯繫我們

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