fbpx

前言

此篇文章將會簡單補充 Python 中時間模組的應用,希望能用最短的時間讓新手們 pick up Python 的時間相關模組~

大綱

  • 時間模組:處理時間格式化需求。
  • 日曆模組:
  • datetime模組:獲取當前時間使用。

一. 時間模組

Python中導入時間模組,語法如下:

範例運作:https://repl.it/@dreamline2/time-module#main.py

  1. 獲取當前時間戳

2. 獲取時間元組:獲取當前時間並以元組方式呈現

3. 獲取格式化時間:時間戳變成格式化時間:time.ctime( )

二. 日曆模組

1.利用 calendar 模塊打印出月曆

範例運作:https://repl.it/@dreamline2/calendar#main.py

三. datetime模組

datetime模組裡面定義了幾種類別,主要獲取當下時間:

  • datetime.date:表示日期。常用的屬性有year, month, day;
  • datetime.time:表示時間。常用的屬性有hour, minute, second, microsecond;
  • datetime.datetime:表示日期時間。
  • datetime.timedelta:表示時間間隔,即兩個時間點之間的長度。
  • datetime.tzinfo:與時區有關的相關資訊。
  • datetime.datetime.now():獲取現在的時間。
  • datetime.date.today():獲取現在的時間。範例運作:https://repl.it/@dreamline2/datetime-example#main.py

 

蝦皮資深工程師來襲🤩,立即點擊圖片獲得更多資訊👆

精彩內容預告:機器學習實作(新手友善)、 Chrome Extension 擴充套件

 

 

celia

celia

人生苦短,我用Python !

Leave a Reply