在本節中,我們將了解如何使用各種方法檢索 Python 的當前日期。日期是應用或網站或資料庫伺服器的重要組成部分,它指示軟體創建的日期和時間、存儲的網站記錄、應用版本等。例如,當用戶使用應用或網站進行某些工作時,軟體會存儲具有特定日期和時間的用戶信息。此外,我們可以說當我們訪問任何社交媒體網站,如推特、臉書、WhatsApp 等。它們存儲用戶的信息,特定用戶的帖子、分享或發送消息的日期等。因此,我們可以定義日期和時間在軟體應用中的重要性。
在 Python 編程語言中,datetime 模塊包含一個用於訪問計算機當前日期和時間的 datetime 類。
使用日期時間. now()方法
Python datetime.now()方法,用於顯示系統的當前日期和時間。它定義了 Python 庫的日期時間模塊中的函數。
語法:
datetime.now()
考慮一個使用 Python 中的 datetime.now()方法訪問系統當前日期和時間的程序。
program.py
import datetime
dt = datetime.datetime.now() # use now() method in datetime
print( "Display the current date of the system: ") # current date
print (str (dt) ) # call the dt variable to print the system date.
輸出:
Display the current date of the system:
2021-02-28 18:56:52.799555
使用 now()方法
now()方法: Python now()方法用於顯示系統當前日期。
語法:
now() # fetch the current date
考慮一個程序,使用 Python 中的 now()方法訪問系統的當前日期和時間。
Program.py
# import the datetime class or module from the datetime module.
from datetime import datetime
now = datetime.now() # Use now() to access the current date and time
print("Current date and time is ", now)
輸出:
Current date and time is 2021-02-28 18:58:33.237779
注意:它通過在 Python 編程中導入 datetime 模塊,以字元串格式返回系統的當前日期。
now()方法的屬性
Python now()方法具有類似於年、月、日、時、分、秒等時間屬性的各種屬性。
讓我們創建一個程序來演示 now()方法的不同屬性,以便在 Python 中列印日期。
程序 2.py
# importing the datetime module for now() method
import datetime
# Use datetime.now() method to get the current date into the variable current_date
current_date = datetime.datetime.now()
# print the message for now() attributes in Python
print (" Following are the attributes of now() function are : ")
# print the current year of the system
print(" The current year is : ", end = "" )
print (current_date.year)
# print the current month of the system
print( " The current month is : ", end = "" )
print (current_date.month)
# print the current date of the system
print( " The current date is : ", end = "" )
print (current_date.day)
輸出:
Following are the attributes of now() function are :
The current year is: 2021
The current month is: 2
The current date is: 28
使用 date.today()方法
出現在日期時間模塊中的日期模塊類,用於返回包含今天日期值的日期對象。其中在 Python 程序中使用 today()方法來獲取今天的日期。
語法:
date.today()
讓我們考慮一個使用 date 類的 today()方法顯示系統當前日期的程序。
程序
from datetime import date
td = date.today() # Use the today method and assign it to the td variable.
print(" Get the today date in Python is: ", td)
輸出:
Get the today date in Python is: 2021-02-28
使用 now()。日期()函數
a 現在()。date()方法,用於通過導入 Python 中的 datetime 模塊來訪問系統的本地或當前日期。
語法:
Current_date = datetime.now().date()
讓我們編寫一個程序,使用 datetime.now()列印系統的當前日期。Python 中的 date() 方法。
程序 3.py
from datetime import datetime
# Return the current date of the system
Current_date = datetime. now(). date()
print("The current date is :", Current_date)
輸出:
The current date is : 2021-02-28
使用 now()。時間()函數
a 現在()。time()方法,用於通過從 Python 中的 datetime 模塊導入 time()方法來訪問系統的本地或當前時間。
語法:
Current_date = datetime.now().time()
讓我們編寫一個程序,使用 datetime.now()列印系統的當前時間。Python 中的 time()方法。
Prog2.py
from datetime import datetime
# Return the current date and time of the system
Current_time = datetime.now().time()
print("Current time is :", Current_time)
輸出:
Current time is: 19:07:45.787512
Python 中的日期和時間格式
下面是日期和時間的各種格式,通過將參數傳遞給str time()函數,可以在 Python 中獲得所需的日期時間表示。
| 管理的 | 描述 | 例子 |
| %a | 顯示縮寫的工作日名稱。 | 珊,我的… |
| %A | 顯示完整的工作日名稱。 | 周日,周一,.. |
| %b | 顯示縮寫的月份名稱。 | 一月,二月,…,十二月 |
| %B | 顯示完整月份的名稱。 | 一月,二月,…十二月 |
| %c | 它用於定位適當的日期和時間表示。 | 孫 2021 年 2 月 21 日 07:05: 28 |
| %d | 以零填充的十進位格式顯示一個月中的某一天。 | 01, 02, …, 28, 30, 31 |
| %H | 以零填充十進位數字的格式顯示小時(24 小時制)。 | 00, 01, …, 23 |
| %I | 它將小時(12 小時制)表示為十進位數。 | 01, 02, …, 12 |
| %j | 以零填充十進位數字的形式顯示一年中的某一天。 | 001, 002, …, 999 |
| %m | 以零填充十進位數的形式顯示月份。 | 01, 02, …, 12 |
| %M | 以零填充十進位數的形式顯示分鐘。 | 00, 01, .., 59 |
| %p | 它用於定位上午或下午的等價物。 | 上午,下午 |
| %S | 以零填充十進位數的形式顯示第二個。 | 00, 01, …, 59 |
| %U | 將星期日算作一年中星期幾的第一天,它以十進位數字顯示這一天。 | 00, 01, …, 53 |
| %w | 它用於將工作日定位為十進位數,其中周日以 0 開頭。 | 00, 01, …, 53 |
| %W | 將星期一算作一年中星期幾的第一天,它以十進位數字顯示這一天。 | 00, 01, …, 53 |
| %x | 它用於定位日期的適當表示。 | 19- 02 – 2021 |
| %X | 它用於定位時間的適當表示。 | 07 : 41: 29 |
| %y | 以十進位數字的形式顯示不包含世紀的年份格式。 | 0, 1, .., 99 |
| %Y | 以十進位數字的形式顯示包含世紀的年份。 | 2014 年、2019 年等。 |
| %z | 它以+HHMM 或- HHMM 的形式表示世界協調時偏移。 | |
| %Z | 它基本上用於表示時區名稱(如果不存在時區,則不包含任何字元)。 | |
| %% | 它只是表示一個字面值「%」字元。 | % |
讓我們創建一個 Python 程序,並使用上述格式。
Prog1.py
import datetime
dt = datetime.datetime.now()
print ("Current date and time is = %s" % dt)
print ("Date and time in ISO Format = %s" % dt.isoformat())
print ("Current year = %s" %dt.year)
print ("Current month is = %s" %dt.month)
print ("Current date (day) = %s" %dt.day )
print ("represent Date in dd/mm/yyyy format = %s / %s/ %s" % (dt.day, dt.month, dt.year))
print ("Current hour is = %s" %dt.hour)
print (" Current minute is = %s" %dt.minute)
print ("Current Second is = %s" %dt.second)
print ("Representation of time in hh: mm: ss format = %s: %s: %s" % (dt.hour, dt.minute, dt.second))
輸出:
Current date and time is = 2021-02-28 19:09:03.739466
Date and time in ISO Format = 2021-02-28T19:09:03.739466
Current year = 2021
Current month is = 2
Current date (day) = 28
Represent Date in dd/mm/yyyy format = 28 / 2/ 2021
Current hour is = 19
Current minute is = 9
Current Second is = 3
Representation of time in hh: mm: ss format = 19: 9: 3
使用 strftime()
函數
讓我們使用 strftime()
函數創建一個顯示當前日期和時間的程序。
Strftime.py
import datetime
dt = datetime.datetime.now()
print (dt.strftime ("%Y - %m - %d %H: %M: %S " )) # represents the date YYYY- MM - DD
# HH: MM: SS
print (dt.strftime ("%d / %m / %Y ")) # represents the date in DD/ MM/ YYYY
print (dt.strftime (" %I: %M: %S %p ")) # represents the time in HH: MM: SS AM/ PM
print (dt.strftime ("%a, %b %d, %Y ")) # represents the day, month date and year
輸出:
2021 - 02 - 28 19: 12: 06
28 / 02 / 2021
07: 12: 06 PM
Sun, Feb 28, 2021
原創文章,作者:簡單一點,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/130560.html