Python程式獲取當前日期時間的各個組成部分
在Python中,我們有一些內建的與時間相關的函式,例如`datetime()`、`today()`、`now()`和`strftime()`,這些函式可以用來獲取當前日期時間的各個組成部分。當前日期時間的各個組成部分指的是精確的當前時間和日期。為了解決這類問題,我們將使用與`datetime`模組相關的一些預定義方法。例如,當前日期和時間對於任何工作環境都很重要。
語法
以下語法在示例中使用:
datetime.now()
這是一個在Python中使用的預定義方法,它包含年份、月份、日期、小時、分鐘和秒。
strftime("%Y-%m-%d %H:%M:%S")
此方法用於透過匯入`datetime`模組來表示日期和時間。
today()
這是一個在Python中使用的預定義方法,它返回當前的本地日期。當定義了`from datetime import date`模組時,可以使用此方法。
now()
`now()`是Python中的一個內建函式,主要用於查詢當前日期和時間。
示例1
在下面的例子中,我們將從匯入名為`datetime`的模組開始程式。然後獲取必要的變數`now`,並使用預定義的`datetime.datetime.now()`方法查詢本地日期和時間。接下來,使用`now`變數(例如`now.year`、`now.month`、`now.day`、`now.hour`、`now.minute`和`now.second`)列印各個組成部分以獲取結果。
import datetime
# Obtain the current date and time
now = datetime.datetime.now()
# Print individual components of current date and time
print("The individual components of the current date and time are:")
print("Year:", now.year)
print("Month:", now.month)
print("Day:", now.day)
print("Hour:", now.hour)
print("Minute:", now.minute)
print("Second:", now.second)
輸出
The individual components of the current date and time are: Year: 2023 Month: 5 Day: 30 Hour: 21 Minute: 31 Second: 26
示例2
在下面的示例中,我們將從匯入`datetime`模組的所有內容開始程式,這將查詢當前時間。接下來,它將從`datetime`模組匯入`date`,這將查詢當前日期。然後初始化名為`tm`的變數,使用`datetime.now()`的預定義方法計算當前時間。接下來,使用`strftime()`方法作為`tm`中的一個物件來查詢時間格式,並將其儲存在`current_time`變數中。最後,我們將分別列印各個日期和時間以獲取結果。
from datetime import datetime
from datetime import date
# Find the current date and time
tm = datetime.now()
# Create the time format in 12-hour format
# %I for 12-hour format, %p for AM/PM
current_time = tm.strftime("%I:%M:%S %p")
# Print individual components
print("The individual components of the current date are:")
print("Year:", tm.year)
print("Month:", tm.month)
print("Day:", tm.day)
print("The individual components of the current time are:")
print("Hour:", tm.strftime("%I"))
print("Minute:", tm.strftime("%M"))
print("Second:", tm.strftime("%S"))
print("AM/PM:", tm.strftime("%p"))
輸出
The individual components of the current date are: Year: 2023 Month: 5 Day: 30 The individual components of the current time are: Hour: 09 Minute: 42 Second: 27 AM/PM: PM
示例3
安裝需求
pip install arrow
執行此示例程式需要上述命令進行安裝。
在下面的例子中,開始匯入名為`arrow`的模組,這將有助於提供其內建函式。然後在變數`date`中使用帶有`arrow`模組的內建函式`now()`。接下來,分別列印日期和時間格式並獲取結果。
import arrow
# Get the current date and time
date = arrow.now()
# Print individual components
print("The individual components of the current date:")
print("Year:", date.year)
print("Month:", date.month)
print("Day:", date.day)
print("The individual components of the current time:")
print("Hour:", date.hour)
print("Minute:", date.minute)
print("Second:", date.second)
輸出
The individual components of the current date: Year: 2023 Month: 5 Day: 30 The individual components of the current time: Hour: 16 Minute: 19 Second: 5
結論
以上兩個輸出顯示了使用與名為`datetime`的模組相關的一些預定義方法來獲取當前日期和時間的結果。對於時間格式的格式化,它使用了`strftime()`方法。此方法也返回日期的格式。還有一個名為`arrow`的模組,它也有助於設定當前日期時間的各個組成部分。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP