在建立現實生活項目時,與時代和日期合作是不可避免的,並且有很多用例。值得慶幸的是,Python有幾個模塊,可以輕鬆地與不同時區的日期和時代一起工作。
> 可以在github上找到本教程的代碼。 >。 內容:
時間模塊
<span>import time as time_module </span> time_in_seconds <span>= time_module.time() </span><span>print("Time in sceconds from epoch", time_in_seconds)</span>
gmtime()函數自從時代開始以來,從秒內以秒的時間表示UTC中的struct_time。 struct_time是一種時間值序列,其命名元組接口由GMTime(),Localtime()和Strptime()返回
Time <span>in sceconds from epoch 1680712853.0801558</span>
localtime()函數
<span>import time as time_module </span> utc_time_in_seconds <span>= time_module.gmtime() </span><span>print("Time struct in UTC", utc_time_in_seconds)</span>
這是以上代碼的輸出:
Time struct <span>in UTC: time.struct_time(tm_year=2023, tm_mon=3, tm_mday=16, tm_hour=14, tm_min=47, tm_sec=28, tm_wday=3, tm_yday=75, tm_isdst=0)</span>
> ctime()方法將從時期開頭的幾秒鐘轉換為字符串格式。如果沒有參數傳遞給該函數,它將在秒內返回當前時間的時間字符串:
<span>import time as time_module </span> local_time <span>= time_module.localtime() </span><span>print("Time struct in local time:", local_time)</span>
strftime()函數
Time struct <span>in local time: time.struct_time(tm_year=2023, tm_mon=4, tm_mday=20, tm_hour=15, tm_min=46, tm_sec=15, tm_wday=3, tm_yday=75, tm_isdst=0)</span>
這是上述代碼的輸出:
<span>import time as time_module </span> time_in_secs <span>= 1678671984.939945 </span> time_string <span>= time_module.ctime(time_in_secs) </span><span>print("Time string: ",time_string)</span>
sleep()函數延遲了指定數量的線程的執行
Time string: Thu Apr <span>20 01:46:24 2023</span>
>在上面的代碼中,數字2作為睡眠()函數的參數傳遞,這會導致循環在執行前延遲兩秒鐘。輸出的數字驗證我們的代碼。
<span>import time as time_module </span> time_tuple <span>= time_module.gmtime() </span>time_format <span>= "%y/%m/%d %I:%M:%S %p" </span> time_in_string <span>= time_module.strftime(time_format, time_tuple) </span> <span>print("Time expressed as formatted string:", time_in_string)</span>
DateTime模塊提供用於操縱日期和時間的類
這些類對於時間間隔,時間和日期的輕鬆操縱,提取和輸出格式至關重要。通常,日期和時間在Python中不被視為數據類型,但它們是DateTime模塊類的日期和時間對象。 DateTime類還具有可用於處理日期和時間對象的不同方法。Time expressed as formatted string: <span>23/04/20 04:40:04 PM</span>
在Python中獲取當前日期和時間
獲得當前日期和時間,請從DateTime模塊導入DateTime類。 DateTime類具有一個方法,即現在(),該類返回當前日期和時間:<span>import time as time_module </span> <span>for i in range(5): </span> local_time <span>= time_module.localtime() </span> seconds <span>= local_time.tm_sec </span> <span>print(seconds) </span> time_module<span>.sleep(2)</span>
這是上述代碼的輸出:
獲得當前日期,請從DateTime模塊導入日期類。日期類有一個方法,今天(),該方法返回當前日期:
>這是上述代碼的輸出:
> DateTime模塊當前有六個類,每個類都有不同的方法來操縱日期和時間對象。這些課程列出如下:
<span>49 </span><span>51 </span><span>53 </span><span>55 </span><span>57</span>
日期對像在理想化的日曆中代表日期(年,月和日) - 當前的Gregorian日曆無限期地擴展了兩個方向。
>可以將日期對象實例化如下:
<span>import time as time_module </span> time_in_seconds <span>= time_module.time() </span><span>print("Time in sceconds from epoch", time_in_seconds)</span>
日期對象構造函數採用三個整數參數,應在指定的範圍內:
或
>示例:獲取當前日期
Time <span>in sceconds from epoch 1680712853.0801558</span>
> today()方法將返回本地日期,而ctime()方法將日期呈現為字符串。 這是以上代碼的輸出:
<span>import time as time_module </span> utc_time_in_seconds <span>= time_module.gmtime() </span><span>print("Time struct in UTC", utc_time_in_seconds)</span>
可以從ISO 8601格式的日期字符串創建日期對象。使用日期類的fromisOformat()方法來執行此操作:
Time struct <span>in UTC: time.struct_time(tm_year=2023, tm_mon=3, tm_mday=16, tm_hour=14, tm_min=47, tm_sec=28, tm_wday=3, tm_yday=75, tm_isdst=0)</span>
<span>import time as time_module </span> local_time <span>= time_module.localtime() </span><span>print("Time struct in local time:", local_time)</span>
>示例:從字符串創建日期對象
Time struct <span>in local time: time.struct_time(tm_year=2023, tm_mon=4, tm_mday=20, tm_hour=15, tm_min=46, tm_sec=15, tm_wday=3, tm_yday=75, tm_isdst=0)</span>
這是上述代碼的輸出:
>要從日期對象提取年,月和一天
<span>import time as time_module </span> time_in_secs <span>= 1678671984.939945 </span> time_string <span>= time_module.ctime(time_in_secs) </span><span>print("Time string: ",time_string)</span>
一個時間對象代表一天中的(本地)時間,獨立於任何特定的一天,並通過tzinfo對象進行調整。
Time string: Thu Apr <span>20 01:46:24 2023</span>
可以將日期對象實例化如下:
<span>import time as time_module </span> time_in_seconds <span>= time_module.time() </span><span>print("Time in sceconds from epoch", time_in_seconds)</span>
可以在沒有任何參數的情況下實例化時間對象。所有參數都是可選的,默認值為0,除了tzinfo,這不是。所有參數必須是指定範圍內的整數,而tzinfo參數應為tzinfo子類的一個實例:
>當不超出範圍的參數傳遞給構造函數時,它會提高價值。
創建一個時間對象,請從DateTime模塊導入時間類。通過幾個小時,分鐘,秒,微秒和tzinfo的爭論。請記住,所有參數都是可選的,因此,當沒有參數傳遞給構造函數時,時間對象返回00:00:00:
Time <span>in sceconds from epoch 1680712853.0801558</span>
這是上述代碼的輸出:
<span>import time as time_module </span> utc_time_in_seconds <span>= time_module.gmtime() </span><span>print("Time struct in UTC", utc_time_in_seconds)</span>
>
Time struct <span>in UTC: time.struct_time(tm_year=2023, tm_mon=3, tm_mday=16, tm_hour=14, tm_min=47, tm_sec=28, tm_wday=3, tm_yday=75, tm_isdst=0)</span>
>示例:從字符串
<span>import time as time_module </span> local_time <span>= time_module.localtime() </span><span>print("Time struct in local time:", local_time)</span>
這是上述代碼的輸出:
Time struct <span>in local time: time.struct_time(tm_year=2023, tm_mon=4, tm_mday=20, tm_hour=15, tm_min=46, tm_sec=15, tm_wday=3, tm_yday=75, tm_isdst=0)</span>
獲取小時,分鐘,秒和微秒
>提取數小時,分鐘,秒和微秒的值,使用時間對象的小時,分鐘,第二和微秒屬性:<span>import time as time_module </span> time_in_secs <span>= 1678671984.939945 </span> time_string <span>= time_module.ctime(time_in_secs) </span><span>print("Time string: ",time_string)</span>
dateTime類
Time string: Thu Apr <span>20 01:46:24 2023</span>
dateTime對像是一個單個對象,其中包含來自日期對象和時間對象的所有信息。
<span>import time as time_module </span> time_tuple <span>= time_module.gmtime() </span>time_format <span>= "%y/%m/%d %I:%M:%S %p" </span> time_in_string <span>= time_module.strftime(time_format, time_tuple) </span> <span>print("Time expressed as formatted string:", time_in_string)</span>
minyear 1 Time expressed as formatted string: <span>23/04/20 04:40:04 PM</span>
1 0
<span>import time as time_module </span> time_in_seconds <span>= time_module.time() </span><span>print("Time in sceconds from epoch", time_in_seconds)</span>
要獲取當前的本地日期和時間,請使用DateTime類的NOW()方法:
Time <span>in sceconds from epoch 1680712853.0801558</span>
>示例:創建從ISO格式創建日期時間
<span>import time as time_module </span> utc_time_in_seconds <span>= time_module.gmtime() </span><span>print("Time struct in UTC", utc_time_in_seconds)</span>
>
Time struct <span>in UTC: time.struct_time(tm_year=2023, tm_mon=3, tm_mday=16, tm_hour=14, tm_min=47, tm_sec=28, tm_wday=3, tm_yday=75, tm_isdst=0)</span>
>獲得的結果非常相似
這是上述代碼的輸出:
>示例:從DateTime對象獲取日期和時間屬性
<span>import time as time_module </span> local_time <span>= time_module.localtime() </span><span>print("Time struct in local time:", local_time)</span>
這是上述代碼的輸出:
Time struct <span>in local time: time.struct_time(tm_year=2023, tm_mon=4, tm_mday=20, tm_hour=15, tm_min=46, tm_sec=15, tm_wday=3, tm_yday=75, tm_isdst=0)</span>
注意:tzinfo的默認屬性值無了,因為沒有傳遞對象參數,並且fold將默認情況下返回0。有關折疊屬性的更多信息(在Python版本3.6中引入),請參見文檔。
the timedelta class<span>import time as time_module </span> time_in_secs <span>= 1678671984.939945 </span> time_string <span>= time_module.ctime(time_in_secs) </span><span>print("Time string: ",time_string)</span>
序列對象代表持續時間,兩個日期或時間之間的差異。
毫秒轉換為1000微秒。
一分鐘轉換為60秒。Time string: Thu Apr <span>20 01:46:24 2023</span>
一個小時轉換為3600秒。
一周轉換為7天。
>示例:創建一個timedelta對象
日期和時間格式因地區到國家和國家 /地區而異。這是因為在日期和時間格式方面存在這些差異,因此引入了ISO 8601格式,作為標準化日期和時間的一種方式。 但是,可能需要根據一個國家或地區以特定方式格式化日期和時間。
使用Strftime()方法方法簽名看起來像這樣:
通常,將字符串格式代碼作為參數傳遞給strftime()方法以格式日期。某些格式代碼如下:
<span>import time as time_module </span> time_in_seconds <span>= time_module.time() </span><span>print("Time in sceconds from epoch", time_in_seconds)</span>
>%a:工作日縮寫名稱 - 例如太陽,蒙上等
這是上述代碼的輸出:
方法簽名看起來像這樣:
Time <span>in sceconds from epoch 1680712853.0801558</span>
將字符串格式代碼作為參數傳遞給strptime()方法以格式日期。
><span>import time as time_module </span> utc_time_in_seconds <span>= time_module.gmtime() </span><span>print("Time struct in UTC", utc_time_in_seconds)</span>
>
這是上述代碼的輸出:
Time struct <span>in UTC: time.struct_time(tm_year=2023, tm_mon=3, tm_mday=16, tm_hour=14, tm_min=47, tm_sec=28, tm_wday=3, tm_yday=75, tm_isdst=0)</span>
一起工作
> python中的時間介紹類用於計算日期之間的差異,計算特定日期之間的時間差,並使用特定的時間單位(例如周或小時)進行其他計算。>
<span>import time as time_module </span> local_time <span>= time_module.localtime() </span><span>print("Time struct in local time:", local_time)</span>
在上面的示例中,我們首先獲得當前的本地日期和時間,以及7天的時間碼對象。由於TimeDERTA支持諸如添加之類的操作,因此我們添加了DateTime對象和TimeDelta對象,以在7天內獲得未來的一天。如果我們的當前日期是2023-04-20,則日期將在2023-04-27之內。
Time struct <span>in local time: time.struct_time(tm_year=2023, tm_mon=4, tm_mday=20, tm_hour=15, tm_min=46, tm_sec=15, tm_wday=3, tm_yday=75, tm_isdst=0)</span>
這是上述代碼的輸出:
使用時區
>示例:將DateTime對像從一個時區轉換為另一個時區
結論 >
可以在github >。
<span>import time as time_module
</span>
time_in_seconds <span>= time_module.time()
</span><span>print("Time in sceconds from epoch", time_in_seconds)</span>
Time <span>in sceconds from epoch 1680712853.0801558</span>
<span>import time as time_module
</span>
utc_time_in_seconds <span>= time_module.gmtime()
</span><span>print("Time struct in UTC", utc_time_in_seconds)</span>
ZoneInfo是一個內置的Python模塊,用於使用時區。 這是以上代碼的輸出:
Time struct <span>in UTC: time.struct_time(tm_year=2023, tm_mon=3, tm_mday=16, tm_hour=14, tm_min=47, tm_sec=28, tm_wday=3, tm_yday=75, tm_isdst=0)</span>
<span>import time as time_module
</span>
local_time <span>= time_module.localtime()
</span><span>print("Time struct in local time:", local_time)</span>
>要在時區之間轉換,我們使用DateTime對象的AstimeZone()方法,以新的時區對像傳遞。 AstimeZone()返回一個帶有更新的時區信息的新的DateTime對象。 Time struct <span>in local time: time.struct_time(tm_year=2023, tm_mon=4, tm_mday=20, tm_hour=15, tm_min=46, tm_sec=15, tm_wday=3, tm_yday=75, tm_isdst=0)</span>
<span>import time as time_module
</span>
time_in_secs <span>= 1678671984.939945
</span>
time_string <span>= time_module.ctime(time_in_secs)
</span><span>print("Time string: ",time_string)</span>
經常詢問有關Python日期和時間的問題(常見問題解答)
>如何將字符串轉換為python中的日期? >在Python中,您可以使用DateTime模塊的Strptime()函數將字符串轉換為日期。此功能需要兩個參數:要轉換的字符串和與字符串的日期格式匹配的格式代碼。例如,如果您的格式“ yyyy-mm-dd”中有一個日期字符串“ 2022-03-01”,則可以將其轉換為這樣的日期:datetime import import import
date_object = dateTime.strptime(date_string,“%y-%m-%d”)
>
我如何獲得python的當前日期和時間? 從dateTime import import dateTime
>我如何在python中格式化一個日期? fort_date_date = currated_date = current_dateTime。 strftime(“%y-%m-%d”)
print(formatted_date)
>我如何從python的日期中添加或減去天數? TIMEDERTA類,您可以用來添加或減去日期的一定天數。以下是您可以使用它的方法:
print > print(new_date)
如何比較python中的兩個日期?以下是一個示例:
>來自dateTime import import dateTime
dateTime(2022,3,1)
>我如何在Python中獲得一周的一天?作為整數的一周(週一為0,星期日為6)。以下是您可以使用它的方法:
>來自dateTime import dateTime
current_date = dateTime.now()
current_date = dateTime.now()
timestamp = current_date.timestamp()
從dateutil import import parser
date_string =“ 2022-03-01”
date_object = parser.parser.parse.parse(date_string)(date_string)
print(date_object)
>如何在Python中獲得當前時間?如果您只需要時間,則可以使用Time()函數這樣:
>如何將時間戳轉換為python中的日期?
>來自DateTime Import DateTime
以上是了解python日期和時間,示例的詳細內容。更多資訊請關注PHP中文網其他相關文章!