Python中日期和時間的用法超強總結
Python 中處理日期和時間的模塊
Python 提供了time
和datetime
模塊,可以幫助我們輕松獲取和修改日期和時間,下面讓我們來逐一了解一下。
time 模塊
該模塊包括使用時間執(zhí)行各種操作所需的所有與時間相關的功能,它還允許我們訪問多種用途所需的時鐘類型。
內(nèi)置函數(shù):
請看下表,它描述了時間模塊的一些重要內(nèi)置功能。
function | Description |
---|---|
time() | 返回自epoch以來經(jīng)過的秒數(shù) |
ctime() | 以經(jīng)過的秒數(shù)作為參數(shù),返回當前日期和時間 |
sleep() | 在給定的持續(xù)時間內(nèi)停止線程的執(zhí)行 |
time.struct_time Class | 函數(shù)要么將此類作為參數(shù),要么將其作為輸出返回 |
localtime() | 以自epoch以來經(jīng)過的秒數(shù)作為參數(shù),并以時間形式返回日期和時間。struct_time格式 |
gmtime() | 與localtime()類似,返回時間。UTC格式的struct_time |
mktime() | ocaltime()的倒數(shù)。獲取包含9個參數(shù)的元組,并返回自epoch pas輸出以來經(jīng)過的秒數(shù) |
asctime() | 獲取包含9個參數(shù)的元組,并返回表示相同參數(shù)的字符串 |
strftime() | 獲取包含9個參數(shù)的元組,并根據(jù)使用的格式代碼返回表示相同參數(shù)的字符串 |
strptime() | 分析字符串并及時返回。struct_time格式 |
代碼格式化:
在用示例解釋每個函數(shù)之前,先看一下所有合法的格式化代碼的方式:
Code | Description | Example |
---|---|---|
%a | Weekday (short version) | Mon |
%A | Weekday (full version) | Monday |
%b | Month (short version) | Aug |
%B | Month (full version) | August |
%c | Local date and time version | Tue Aug 23 1:31:40 2019 |
%d | Depicts the day of the month (01-31) | 07 |
%f | Microseconds | 000000-999999 |
%H | Hour (00-23) | 15 |
%I | Hour (00-11) | 3 |
%j | Day of the year | 235 |
%m | Month Number (01-12) | 07 |
%M | Minutes (00-59) | 44 |
%p | AM / PM | AM |
%S | Seconds (00-59) | 23 |
%U | Week number of the year starting from Sunday (00-53) | 12 |
%w | Weekday number of the week | Monday (1) |
%W | Week number of the year starting from Monday (00-53) | 34 |
%x | Local date | 06/07/22 |
%X | Local time | 12:30:45 |
%y | Year (short version) | 22 |
%Y | Year (full version) | 2022 |
%z | UTC offset | +0100 |
%Z | Timezone | CST |
%% | % Character | % |
struct_time 類具有以下屬性:
Attribute | Value |
---|---|
tm_year | 0000, .., 2019, …, 9999 |
tm_mon | 1-12 |
tm_mday | 1-31 |
tm_hour | 0-23 |
tm_min | 0-59 |
tm_sec | 0-61 |
tm_wday | 0-6 (Monday is 0) |
tm_yday | 1-366 |
tm_isdst | 0, 1, -1 (daylight savings time, -1 when unknown) |
現(xiàn)在讓我們看幾個 time
模塊的例子
使用time模塊查找日期和時間
使用上表中描述的內(nèi)置函數(shù)和格式化代碼,可以在 Python 中輕松獲取日期和時間。
import?time #time a=time.time()???????????#total?seconds?since?epoch print("Seconds?since?epoch?:",a,end='n----------n') #ctime print("Current?date?and?time:") print(time.ctime(a),end='n----------n')? #sleep time.sleep(1)?????#execution?will?be?delayed?by?one?second #localtime print("Local?time?:") print(time.localtime(a),end='n----------n') #gmtime print("Local?time?in?UTC?format?:") print(time.gmtime(a),end='n-----------n') #mktime b=(2019,8,6,10,40,34,1,218,0) print("Current?Time?in?seconds?:") print(?time.mktime(b),end='n----------n') #asctime print("Current?Time?in?local?format?:") print(?time.asctime(b),end='n----------n') #strftime c?=?time.localtime()?#?get?struct_time d?=?time.strftime("%m/%d/%Y,?%H:%M:%S",?c) print("String?representing?date?and?time:") print(d,end='n----------n') #strptime print("time.strptime?parses?string?and?returns?it?in?struct_time?format?:n") e?=?"06?AUGUST,?2019" f?=?time.strptime(e,?"%d?%B,?%Y") print(f)
Output:
Seconds since epoch : 1565070251.7134922
———-
Current date and time:
Tue Aug 6 11:14:11 2019
———-
Local time :
time.struct_time(tm_year=2019, tm_mon=8, tm_mday=6, tm_hour=11, tm_min=14, tm_sec=11, tm_wday=1, tm_yday=218, tm_isdst=0)
———-
Local time in UTC format :
time.struct_time(tm_year=2019, tm_mon=8, tm_mday=6, tm_hour=5, tm_min=44, tm_sec=11, tm_wday=1, tm_yday=218, tm_isdst=0)
———–
Current Time in seconds :
1565068234.0
———-
Current Time in local format :
Tue Aug 6 10:40:34 2019
———-
String representing date and time:
08/06/2019, 11:14:12
———-
time.strptime parses string and returns it in struct_time format :
time.struct_time(tm_year=2019, tm_mon=8, tm_mday=6, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=218, tm_isdst=-1)
datetime 模塊
與time
模塊類似,datetime
模塊包含處理日期和時間所必需的所有方法。
內(nèi)置功能:
下表介紹了本模塊中的一些重要功能:
function | Description |
---|---|
datetime() | datetime 的構造函數(shù) |
datetime.today() | 返回當前本地日期和時間 |
datetime.now() | 返回當前本地日期和時間 |
date() | 以年、月、日為參數(shù),創(chuàng)建相應的日期 |
time() | 以小時、分鐘、秒、微秒和tzinfo作為參數(shù),并創(chuàng)建相應的日期 |
date.fromtimestamp() | 轉(zhuǎn)換秒數(shù)以返回相應的日期和時間 |
timedelta() | 它是不同日期或時間之間的差異(持續(xù)時間) |
使用 datetime 查找日期和時間
現(xiàn)在,讓我們嘗試實現(xiàn)這些函數(shù),以使用datetime
模塊在 Python 中查找日期和時間。
import?datetime #datetime?constructor print("Datetime?constructor:n") print(datetime.datetime(2019,5,3,8,45,30,234),end='n----------n') ? #today print("The?current?date?and?time?using?today?:n") print(datetime.datetime.today(),end='n----------n') ? #now print("The?current?date?and?time?using?today?:n") print(datetime.datetime.now(),end='n----------n') ? #date print("Setting?date?:n") print(datetime.date(2019,11,7),end='n----------n') ?? #time print("Setting?time?:n") print(datetime.time(6,30,23),end='n----------n') ? #date.fromtimestamp print("Converting?seconds?to?date?and?time:n") print(datetime.date.fromtimestamp(23456789),end='n----------n') ? #timedelta b1=datetime.timedelta(days=30,?seconds=0,?microseconds=0,?milliseconds=0,?minutes=0,?hours=4,?weeks=8) b2=datetime.timedelta(days=3,?seconds=0,?microseconds=0,?milliseconds=0,?minutes=0,?hours=4,?weeks=8) b3=b2-b1 print(type(b3)) print("The?resultant?duration?=?",b3,end='n----------n') ? #attributes a=datetime.datetime.now()???#1 print(a) print("The?year?is?:",a.year) ? print("Hours?:",a.hour)
Output:
Datetime constructor:
2019-05-03 08:45:30.000234
———-
The current date and time using today :
2019-08-06 13:09:56.651691
———-
The current date and time using today :
2019-08-06 13:09:56.651691
———-
Setting date :
2019-11-07
———-
Setting time :
06:30:23
———-
Converting seconds to date and time:
1970-09-29
———-
<class ‘datetime.timedelta’>
The resultant duration = -27 days, 0:00:00
———-
2019-08-06 13:09:56.653694
The year is : 2019
Hours : 13
到此這篇關于Python中日期和時間的用法超強總結的文章就介紹到這了,更多相關Python日期 時間用法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Windows上配置Emacs來開發(fā)Python及用Python擴展Emacs
這篇文章主要介紹了Windows上配置Emacs來開發(fā)Python及用Python擴展Emacs的方法,Emacs與Vim并稱為開發(fā)者手中最強大的兩款文本編輯器,需要的朋友可以參考下2015-11-11Windows下用py2exe將Python程序打包成exe程序的教程
這篇文章主要介紹了Windows下用py2exe將Python程序打包成exe程序的教程,文中主要針對Python3.x版本進行說明,需要的朋友可以參考下2015-04-04Python?Pexpect庫自動化交互式進程控制的expect_list方法解析
Pexpect是一個Python庫,為自動化和交互式進程控制提供了豐富的功能,而expect_list方法是其功能強大且靈活的一部分,將詳細探討如何使用這一方法,并提供多個示例來說明其應用場景和功能2024-01-01python中創(chuàng)建以及刪除虛擬環(huán)境的幾種方法總結
在Python?中創(chuàng)建虛擬環(huán)境非常容易,但是刪除虛擬環(huán)境可能會有一些挑戰(zhàn),這篇文章主要給大家介紹了關于python中創(chuàng)建以及刪除虛擬環(huán)境的幾種方法,需要的朋友可以參考下2024-03-03使用Pandas實現(xiàn)可視化帶有標簽列的數(shù)據(jù)表
Pandas是Python中一個靈活強大的數(shù)據(jù)處理庫,它提供了大量數(shù)據(jù)操作和分析工具,本文我們將討論如何使用Pandas可視化帶有標簽列的數(shù)據(jù)表,以便更好地呈現(xiàn)和傳達數(shù)據(jù)的信息,需要的可以了解下2024-02-02