了解python日期和時間,示例
在建立現實生活項目時,與時代和日期合作是不可避免的,並且有很多用例。值得慶幸的是,Python有幾個模塊,可以輕鬆地與不同時區的日期和時代一起工作。
> 可以在github上找到本教程的代碼。 >。 內容:
時間模塊
- time()函數
- gmtime()函數
- localtime()函數
- > ctime()函數
- strftime()函數
- 睡眠()函數
- 在Python中獲取當前日期和時間 在python
- 中獲取當前日期 dateTime模塊類
- 日期類
- 時間類
- dateTime類
- the timedelta class
- python dateTime格式
- 使用Strftime()方法 格式化日期> 使用Strptime()方法
- 格式化日期>
- 與TimeDelta
- 一起工作
- Python提供`dateTime'和`time'模塊來處理日期和時間操作,這對於現實生活項目至關重要。 `dateTime`模塊都包括``date'',time',`dateTime`,`timeDeTim','timeDelta',`tzinfo'和`timeZone''和`timezone''的類。
`termedelta'類可用於計算日期和時間的差異,允許添加,減法和其他基於時間的計算。
時區處理由`ZoneInfo`模塊促進,從而實現了TimeZone Aware Aware Aware DateTime對象的創建。 Python的日期和時間格式遵守ISO 8601標準的方法,但也允許自定義適合區域偏好。- 時間模塊 Python時間模塊用於執行時間相關的操作。現在,我們將在時間模塊中重點介紹一些最常用的功能,其中包括示例。
- time()函數
- time()函數自集合時期作為浮點數以來,返回當前時間。使用的時期從1970年1月1日開始,00:00:00(UTC):
- 這是上述代碼的輸出:
<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()函數
gmtime()函數自從時代開始以來,從秒內以秒的時間表示UTC中的struct_time。 struct_time是一種時間值序列,其命名元組接口由GMTime(),Localtime()和Strptime()返回
這是以上代碼的輸出: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>
登入後複製登入後複製登入後複製登入後複製登入後複製登入後複製localtime()函數自時代開始以來的本地時間內返回struct_time。
這是以上代碼的輸出:
> ctime()函數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>
登入後複製登入後複製登入後複製登入後複製登入後複製登入後複製這是上述代碼的輸出:
strftime()函數
strftime()方法將struct_time轉換為給定格式參數指定的時間字符串: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>
登入後複製登入後複製登入後複製登入後複製<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模塊
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>
登入後複製這是上述代碼的輸出:
在python
中獲取當前日期獲得當前日期,請從DateTime模塊導入日期類。日期類有一個方法,今天(),該方法返回當前日期:
><span>49 </span><span>51 </span><span>53 </span><span>55 </span><span>57</span>
登入後複製- > date
- 時間
- dateTime
- timedelta
- > tzinfo
- 時區
日期類
日期對像在理想化的日曆中代表日期(年,月和日) - 當前的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>
登入後複製登入後複製登入後複製登入後複製登入後複製登入後複製日期對象構造函數採用三個整數參數,應在指定的範圍內:
> - minyear
1
1
在上面的代碼中,minyear為1,Maxyear為9999。該值代表了A
或
這是以上代碼的輸出:
>示例:獲取當前日期
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格式的日期
可以從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子類的一個實例:
- 0
- 0
- 0
- 0
>當不超出範圍的參數傳遞給構造函數時,它會提高價值。
>示例:創建一個時間對象
創建一個時間對象,請從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>
可以從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>
創建一個時間對象,將日期字符串和相應格式傳遞給Strptime()方法。使用返回的DateTime對象的time()方法提取時間:
這是上述代碼的輸出:
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>
dateTime構造函數需要年度,月和日論點。 tzinfo默認值不或tzinfo子類的實例。時間參數是可選的,但是參數必須是整數,並且在範圍內:
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>
>從ISO 8601格式的日期時間字符串創建DateTime對象,請使用DateTime類的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>
>獲得的結果非常相似
這是上述代碼的輸出:
>示例:從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天。
- 所有參數均應在文檔中指定的以下範圍內:
> - 0
- 0 >
- -999999999
>示例:創建一個timedelta對象
- >創建一個timedelta對象,請從datetime模塊導入timedelta類。將適當的參數傳遞給構造函數函數:
- 這是上述代碼的輸出:
- python dateTime格式
日期和時間格式因地區到國家和國家 /地區而異。這是因為在日期和時間格式方面存在這些差異,因此引入了ISO 8601格式,作為標準化日期和時間的一種方式。 但是,可能需要根據一個國家或地區以特定方式格式化日期和時間。
使用Strftime()方法格式化日期>
可以使用strftime()方法來完成 dateTime格式。 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:工作日縮寫名稱 - 例如太陽,蒙上等
- %b:月份為縮寫名稱 - 例如1月,2月等
- %y:一年沒有世紀作為零填充的十進制數字 - 例如00、01、02等
- 可以在Python文檔中找到帶有格式代碼的更詳細的表格。 >示例:dateTime對像中的格式日期和時間
-
>與以前的示例一樣,我們可以將所需日期和時間輸出格式字符串的參數傳遞給strftime()方法: >
這是上述代碼的輸出:
格式化日期> 與Strftime()不同,Strptime()是DateTime類方法,這意味著可以在不創建類的對象的情況下使用它。該方法從給定的日期字符串和格式返回DateTime對象。
方法簽名看起來像這樣:
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>
要創建一個DateTime對象,我們將將兩個參數傳遞給Strptime()方法,日期字符串和相應的格式。當日期字符串與提供的格式不匹配時,會提高value error:
>
這是上述代碼的輸出:
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類,您可以用來添加或減去日期的一定天數。以下是您可以使用它的方法:
>來自dateTime import dateTime,timeDELTA
current_date = dateTime.now() 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()
>如何從Python中的字符串中解析一個日期?以下是您可以使用它的方法:
從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中的日期?
>在Python中,您可以使用dateTime.fromtimestamp()函數將時間戳轉換為日期。這是一個示例:
>來自DateTime Import DateTime
以上是了解python日期和時間,示例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

該試點程序是CNCF(雲本機計算基礎),安培計算,Equinix金屬和驅動的合作,簡化了CNCF GitHub項目的ARM64 CI/CD。 該計劃解決了安全問題和績效

該教程通過使用AWS服務來指導您通過構建無服務器圖像處理管道。 我們將創建一個部署在ECS Fargate群集上的next.js前端,與API網關,Lambda函數,S3桶和DynamoDB進行交互。 Th

與這些頂級開發人員新聞通訊有關最新技術趨勢的了解! 這個精選的清單為每個人提供了一些東西,從AI愛好者到經驗豐富的後端和前端開發人員。 選擇您的收藏夾並節省時間搜索REL

Arm64 架構開源軟件的 CI/CD 難題與解決方案 在 Arm64 架構上部署開源軟件需要一個強大的 CI/CD 環境。然而,Arm64 和傳統 x86 處理器架構的支持水平之間存在差異,Arm64 通常處於劣勢。面向多種架構的基礎設施組件開發人員對工作環境有一定的期望: 一致性:跨平台使用的工具和方法保持一致,避免因採用不太流行的平台而需要改變開發流程。 性能:平台和支持機制具有良好的性能,確保在支持多個平台時部署方案不會因速度不足而受影響。 測試覆蓋率:對所有平台同時進行效率、合規性和

定制电信软件开发无疑是一项相当大的投资。然而,从长远来看,您可能会意识到,这样的项目可能更具成本效益,因为它可以像市场上任何现成的解决方案一样提高您的生产力。了解构建定制电信系统的最重要优势。 获取您所需的确切功能 您可以购买的现成电信软件有两个潜在问题。有些缺乏可能显著改善您工作效率的有用功能。有时您可以通过一些外部集成来增强它们,但这并不总是足以使它们变得出色。 其他软件功能过多,使用起来过于复杂。您可能不会使用其中的一些(永远不会!)。大量的功能通常还会增加价格。 基于您的需求
