如何在 Python 中獲取格式化的日期和時間?
在本文中,我們將向您展示如何在 python 中格式化日期和時間。
Python 中的datetime 模組 提供了處理日期和時間值的方法。要使用此模組,我們必須首先使用以下import 關鍵字匯入它:
import datetime
strftime 函式()
strftime() 函式返回格式化的日期和時間。它接受一個格式字串,您可以使用它來獲取所需的結果。以下是它支援的指令。
| 指令 | 含義 |
|---|---|
| %a | 本地化簡寫的星期名稱 |
| %B | 本地化的完整月份名稱。 |
| %c | 本地化合適的日期和時間表示 |
| %d | 月份中的日期,以十進位制數字表示 [01,31]。 |
| %H | 小時(24 小時制),以十進位制數字表示 [00,23]。 |
| %I | 小時(12 小時制),以十進位制數字表示 [01,12]。 |
| %m | 月份,以十進位制數字表示 [01,12]。 |
| %M | 分鐘,以十進位制數字表示 [00,59]。 |
| %p | 本地化等效於 AM 或 PM。 |
| %S | 秒,以十進位制數字表示 [00,61]。 |
| %U | 一年中的星期數(星期日作為一週的第一天),以十進位制數字表示 [00,53]。新一年中第一個星期日之前的所有日期都被認為是在第 0 周。 |
| %w | 星期幾,以十進位制數字表示 [0(星期日),6]。 |
| %W | 一年中的星期數(星期一作為一週的第一天),以十進位制數字表示 [00,53]。新一年中第一個星期一之前的所有日期都被認為是在第 0 周。 |
| %x | 本地化合適的日期表示。 |
| %X | 本地化合適的時間表示。 |
| %y | 不帶世紀的年份,以十進位制數字表示 [00,99]。 |
| %Y | 帶世紀的年份,以十進位制數字表示。 |
| %Z | 時區名稱(如果不存在時區,則不顯示字元)。 |
| %% | 文字“%”字元 |
datetime 類的 Strftime() 函式
使用date、time或datetime物件,strftime()方法返回一個表示日期和時間的字串。
要使用指定的格式將 datetime 物件轉換為字串,請使用datetime.strftime(format)。
格式程式碼是用於指定要表示 datetime 的格式的標準指令。例如,%d-%m-%Y%H:%M:%S程式碼將日期轉換為dd-mm-yyyy hh:mm:ss格式。
示例
from datetime import datetime # getting the current date and time current_datetime = datetime.now() # getting the year from the current date and time current_year = current_datetime.strftime("%Y") print("current year = ", current_year) # getting the month from the current date and time current_month = current_datetime.strftime("%m") print("current month = ", current_month) # getting the day/date from the current date and time current_day = current_datetime.strftime("%d") print("current day = ", current_day) # getting the time from the current date and time in the given format current_time = current_datetime.strftime("%H:%M:%S") print("current time = ", current_time) # getting the date and time from the current date and time in the given format current_date_time = current_datetime.strftime("%m/%d/%Y, %H:%M:%S") print("current date and time = ",current_date_time)
輸出
current year = 2022 current month = 08 current day = 15 current time = 17:49:46 current date and time = 08/15/2022, 17:49:46
注意:如果程式在線上編輯器上執行,它將顯示 GMT 時間,而如果程式在本地系統編輯器上執行,它將顯示您自己的時間
Time 類
time類可用於表示時間值。小時、分鐘、秒和微秒是 time 類屬性。
語法
time(hour, minute, second, microsecond)
示例 1
import datetime # formating time format_time = datetime.time(3, 12, 24, 10) print(format_time)
輸出
03:12:24.000010
存在時間屬性範圍,例如,秒的範圍為 0 到 59,納秒的範圍為 0 到 999999。如果範圍過大,編譯器將顯示 ValueError
time 類的例項具有三個例項屬性:小時、分鐘、秒和微秒。這些用於獲取特定時間資訊
示例 2
import datetime # formating time format_time = datetime.time(3, 12, 24, 10) # Printing time in the formating of time attributes print('Time:', format_time.hour,' hours ', format_time.minute,' minutes ', format_time.second,'seconds and ', format_time.microsecond,'microseconds' )
輸出
Time: 3 hours 12 minutes 24 seconds and 10 microseconds
Date 類
可以使用 date 類表示日曆日期值。date 例項由年份、月份和日期屬性組成。
語法
date(yyyy, mm, dd)
示例 1
import datetime # formating date format_date = datetime.date(2005, 4, 16) # Printing date in the formating of date attributes print('Date:', format_date.day, 'day,', format_date.month, 'month and', format_date.year, 'year')
輸出
Date: 16 day, 4 month and 2005 year
將日期轉換為字串
因為日期和時間與字串不同,所以經常需要將 datetime 轉換為字串。我們為此使用 strftime() 函式。
語法
time.strftime(format, t)
引數
format- 字串型別。指令可以包含在格式字串中
time - 應格式化的時間
示例
import datetime # formating date and time format_datetime = datetime.datetime(2005, 4, 16, 3, 22, 30, 8) # Printing date & time in the formating of date and time attributes print(format_datetime.strftime("%b %d %Y %H:%M:%S"))
輸出
Apr 16 2005 03:22:30
符號 %H、%M 和 %S 分別表示小時、分鐘和秒。字元 %b、%d 和 %Y 分別表示月份、日期和年份。
將字串轉換為日期
在處理從 csv 匯入的資料集或從網站表單獲取輸入時,經常需要進行從字串到日期的轉換。Python 有一個名為strptime()的函式來完成此操作。
語法
datetime.strptime(string, format)
引數
string- 應格式化的字串
format - 字串型別。指令可以包含在格式字串中。
示例
from datetime import datetime # converting input string to date format(date-month-year format) print(datetime.strptime('16/5/2005', '%d/%m/%Y'))
輸出
2005-05-16 00:00:00
結論
我們學習瞭如何使用 datetime 模組在 Python 中格式化日期和時間。我們還學習瞭如何透過將日期轉換為字串然後轉換回日期來格式化日期。
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP