在本教程中,我們將了解 Python 編程語言中str time()函數的工作原理及其變體。
所以,讓我們開始吧。
正如我們已經知道的,Python 提供了各種模塊,這些模塊包含函數集群,以便在數據上實現不同類型的功能。利用 Python 的時間模塊實現基於各種時間戳的操作。
此外,Python 的 strftime() 函數接受不同格式的時間,並返回一個以標準形式表示時間的字符串。
我們可以將 Pythonstr time()函數與 datetime 模塊一起使用,以便按照格式代碼以適當的格式獲取當前時間戳。
其語法如下所示。
語法:
datetime.now().strftime('format codes')
這裡,str time()函數的參數,格式代碼實際上是為了以標準和適當的方式表示時間戳而使用的預定義代碼。在本教程中,我們將更詳細地了解格式代碼。
但在此之前,讓我們考慮一個例子。
示例:
# importing the datetime module
from datetime import datetime
# storing the value of current timestamp
cur_timestamp = datetime.now()
# using the strftime() function to represent
# timestamp into proper and standard format
the_time = cur_timestamp.strftime( "%H : %M : %S" )
the_date = cur_timestamp.strftime( "%d - %m - %Y" )
# printing the values
print("Present Time:", the_time)
print("Present Date:", the_date)
輸出:
Present Time: 18 : 06 : 17
Present Date: 01 - 06 - 2021
說明:
在上例中,我們從日期時間庫中導入了日期時間模塊。然後,我們定義了一個存儲當前日期和時間值的變量。然後,我們使用 strftime() 功能將當前日期和時間表示為指定的標準格式,並將其打印給用戶。因此,當前日期和時間已成功地以適當的標準格式打印出來。
有時,當我們想要顯示過去數據集的日期和時間時,就會出現這種情況。藉助 Python 中的str time()函數,我們可以執行同樣的操作。
日期時間模塊的frontimestamp()方法幫助用戶獲取預定義的時間戳。此外,我們可以使用 strftime() 函數,以便使用前面解釋的不同格式代碼以標準格式表示提取的時間戳。
我們可以看到str time()函數用法的語法如下:
語法:
datetime.fromtimestamp(timestamp).strftime('format code')
現在,讓我們考慮一個示例,說明str time()函數的工作以及預定義的時間戳。
示例:
# importing the datetime module
from datetime import datetime
# provided timestamp
provided_timestamp = 114579923
# storing the value of provided timestamp
my_timestamp = datetime.fromtimestamp(provided_timestamp)
# using the strftime() function to represent
# timestamp into proper and standard format
the_time = my_timestamp.strftime( "%H : %M : %S" )
the_date = my_timestamp.strftime( "%d - %m - %Y" )
# printing the values
print("Time as per the provided Timestamp:", the_time)
print("Date as per the provided Timestamp:", the_date)
輸出:
Time as per the provided Timestamp: 09 : 15 : 23
Date as per the provided Timestamp: 19 - 08 - 1973
說明:
在上面的例子中,我們已經定義了存儲預定義時間戳值的變量。然後,我們使用frontime stamp()方法獲取存儲的值,並使用 strftime() 函數將獲取的時間戳表示為適當的標準格式。然後,我們為用戶打印了結果值。因此,時間戳已成功地以正確的指定格式表示。
pythonstr time()函數利用不同的格式代碼,以維護和標準的形式表示日期和時間。此外,這些格式代碼可用於將小時、天、周等與時間戳分隔開來,並顯示相同的內容。
讓我們考慮一個表示格式代碼用法的例子。
示例:
# importing the strftime function
from time import strftime
# using "%A" as the format code
# "%A" displays the current day of the local time
the_day = strftime( "%A" )
# printing the values
print("Present Day:", the_day)
輸出:
Present Day: Tuesday
說明:
在上面的例子中,我們已經從時間模塊導入了時間功能。然後,我們定義了一個變量,該變量使用 strftime() 函數以及參數“ %A ”作為當地時間當天的格式代碼。然後,我們為用戶打印了這些值。因此,程序相應地打印了當天的全名。
現在讓我們考慮一些使用其他格式代碼的例子。
1.使用“ %c ”作為格式碼,以如下格式顯示當前當地時間:
日月日期小時:分鐘:秒年
示例:
# importing the strftime function
from time import strftime
# using "%c" as the format code
# "%c" displays the current local time
the_timestamp = strftime( "%c" )
# printing the values
print("Present Timestamp:", the_timestamp)
輸出:
Present Timestamp: Wed Jun 2 16:06:41 2021
說明:
在上面的程序中,我們再次執行了與前面相同的程序。但是我們用了“ %c ”作為格式碼。結果,程序以上面指定的格式代碼打印了當前時間戳。
2.使用“ %R ”作為格式代碼,以便以 24 小時格式表示時間。
示例:
# importing the strftime function
from time import strftime
# using "%R" as the format code
# "%R" displays the current time in 24-hour format
the_24hour = strftime( "%R" )
# printing the values
print("Present Time in 24-hour format:", the_24hour)
輸出:
Present Time in 24-hour format: 16:14
說明:
我們在上面的程序中再次遵循了相同的語法,但是我們包含了格式代碼“ %R ”以 24 小時格式顯示當前時間。
3.使用“ %r ”作為格式代碼,以 12 小時格式或小時:分:秒格式以及上午或下午符號表示時間。
示例:
# importing the strftime function
from time import strftime
# using "%r" as the format code
# "%r" displays the current local time in H:M:S format
the_time = strftime( "%r" )
# printing the values
print("Present Time in 12-hour format:", the_time)
輸出:
Present Time in 12-hour format: 04:33:12 PM
說明:
與我們之前所做的類似,我們添加了“ %r ”作為str time()函數的參數,以 12 小時格式打印當前時間。
4.在一個函數中使用多個格式代碼。
示例:
# importing the strftime function
from time import strftime
# using multiple format code
# "%x" displays the current date
# "%X" displays the current time in 24-hour format
# "%p" provides discriptions for current time i.e., AM or PM
the_timestamp = strftime( "%x -- %X %p" )
# printing the values
print("Present Timestamp:", the_timestamp)
輸出:
Present Timestamp: 06/02/21 -- 16:44:11 PM
說明:
在上面的例子中,為了顯示來自本地時間戳的日期,我們使用了格式代碼“ %x ”。我們還使用了格式代碼“ %X ”以 24 小時格式顯示當前時間,同時使用了格式代碼“ %p ”以提供當前時間的符號,即上午或下午。
除了上述格式代碼,還有許多格式代碼可用。下面列出了這些格式代碼:
| 南號碼 | 格式代碼 | 描述 |
| one | %a | 簡而言之,工作日名稱 |
| Two | %A | 完整的工作日名稱 |
| three | %b | 簡而言之,月份名稱 |
| four | %B | 完整的月份名稱 |
| five | %c | 選定的日期和時間表示 |
| six | %C | 世紀數(年除以 100,範圍從 00 到 99) |
| seven | %d | 月日(從 01 到 31) |
| eight | %D | 類似功能為 %m / %d / %y |
| nine | %e | 月日(從 1 到 31) |
| Ten | %g | 功能與 %G 類似;然而,價值是印刷沒有世紀 |
| Eleven | %G | 相當於 ISO 周數的 4 位數年份(參考 %V ) |
| Twelve | %h | 功能與 %b 相似 |
| Thirteen | %H | 小時,使用 24 小時制(從 00 到 23) |
| Fourteen | %I | 小時,使用 12 小時制(從 01 到 12) |
| Fifteen | %j | 年日(從 001 到 366) |
| Sixteen | %m | 月份(從 01 到 12) |
| Seventeen | %M | 分鐘 |
| Eighteen | %n | 換行符 |
| Nineteen | %p | 根據提供的時間值,上午或下午 |
| Twenty | %r | 以上午和下午符號表示的時間 |
| Twenty-one | %R | 24 小時制時間 |
| Twenty-two | %S | 第二 |
| Twenty-three | %t | 製表符 |
| Twenty-four | %T | 當前時間,等於“% H:% M:% S”格式 |
| Twenty-five | %u | 以數字(範圍從 1 到 7)表示的星期幾,其中星期一被認為是 1。警告:在 Sun Solaris 中,星期日被視為 1。 |
| Twenty-six | %U | 當前年份的周數,從第一個星期日作為第一周的第一天開始。 |
| Twenty-seven | %V | 當前年份的 ISO 8601 周數(範圍從 01 到 53),其中第 1 周是當前年份中至少有四天的第一周,星期一被視為第一周 |
| Twenty-eight | %w | 以十進制表示的一周中的某一天,其中星期日被認為是 0。 |
| Twenty-nine | %W | 當前年份的周數,從第一個星期一作為第一周的第一天開始。 |
| Thirty | %x | 沒有時間的日期選擇描述 |
| Thirty-one | %X | 沒有日期的時間選擇描述 |
| Thirty-two | %y | 缺少一個世紀的年份(從 00 到 99) |
| Thirty-three | %Y | 國際年與世紀 |
| Thirty-four | %z 或%Z | 時區、名稱或縮寫 |
| Thirty-five | %% | 實際的%字符 |
原創文章,作者:T7KXI,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/126182.html