最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python中日期和時(shí)間的用法超強(qiáng)總結(jié)

 更新時(shí)間:2022年10月17日 10:24:47   作者:蘿卜大雜燴  
時(shí)間無(wú)疑是生活各個(gè)方面中最關(guān)鍵的因素之一,因此,記錄和跟蹤時(shí)間變得非常重要。在?Python?中,可以通過(guò)其內(nèi)置庫(kù)跟蹤日期和時(shí)間。今天我們來(lái)介紹關(guān)于?Python?中的日期和時(shí)間,一起來(lái)了解如何使用time和datetime模塊查找和修改日期和時(shí)間

Python 中處理日期和時(shí)間的模塊

Python 提供了timedatetime模塊,可以幫助我們輕松獲取和修改日期和時(shí)間,下面讓我們來(lái)逐一了解一下。

time 模塊

該模塊包括使用時(shí)間執(zhí)行各種操作所需的所有與時(shí)間相關(guān)的功能,它還允許我們?cè)L問(wèn)多種用途所需的時(shí)鐘類型。

內(nèi)置函數(shù):

請(qǐng)看下表,它描述了時(shí)間模塊的一些重要內(nèi)置功能。

functionDescription
time()返回自epoch以來(lái)經(jīng)過(guò)的秒數(shù)
ctime()以經(jīng)過(guò)的秒數(shù)作為參數(shù),返回當(dāng)前日期和時(shí)間
sleep()在給定的持續(xù)時(shí)間內(nèi)停止線程的執(zhí)行
time.struct_time Class函數(shù)要么將此類作為參數(shù),要么將其作為輸出返回
localtime()以自epoch以來(lái)經(jīng)過(guò)的秒數(shù)作為參數(shù),并以時(shí)間形式返回日期和時(shí)間。struct_time格式
gmtime()與localtime()類似,返回時(shí)間。UTC格式的struct_time
mktime()ocaltime()的倒數(shù)。獲取包含9個(gè)參數(shù)的元組,并返回自epoch pas輸出以來(lái)經(jīng)過(guò)的秒數(shù)
asctime()獲取包含9個(gè)參數(shù)的元組,并返回表示相同參數(shù)的字符串
strftime()獲取包含9個(gè)參數(shù)的元組,并根據(jù)使用的格式代碼返回表示相同參數(shù)的字符串
strptime()分析字符串并及時(shí)返回。struct_time格式

代碼格式化:

在用示例解釋每個(gè)函數(shù)之前,先看一下所有合法的格式化代碼的方式:

CodeDescriptionExample
%aWeekday (short version)Mon
%AWeekday (full version)Monday
%bMonth (short version)Aug
%BMonth (full version)August
%cLocal date and time versionTue Aug 23 1:31:40 2019
%dDepicts the day of the month (01-31)07
%fMicroseconds000000-999999
%HHour (00-23)15
%IHour (00-11)3
%jDay of the year235
%mMonth Number (01-12)07
%MMinutes (00-59)44
%pAM / PMAM
%SSeconds (00-59)23
%UWeek number of the year starting from Sunday (00-53)12
%wWeekday number of the weekMonday (1)
%WWeek number of the year starting from Monday (00-53)34
%xLocal date06/07/22
%XLocal time12:30:45
%yYear (short version)22
%YYear (full version)2022
%zUTC offset+0100
%ZTimezoneCST
%%% Character%

struct_time 類具有以下屬性:

AttributeValue
tm_year0000, .., 2019, …, 9999
tm_mon1-12
tm_mday1-31
tm_hour0-23
tm_min0-59
tm_sec0-61
tm_wday0-6  (Monday is 0)
tm_yday1-366
tm_isdst0, 1, -1    (daylight savings time, -1 when unknown)

現(xiàn)在讓我們看幾個(gè) time 模塊的例子

使用time模塊查找日期和時(shí)間

使用上表中描述的內(nèi)置函數(shù)和格式化代碼,可以在 Python 中輕松獲取日期和時(shí)間。

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模塊包含處理日期和時(shí)間所必需的所有方法。

內(nèi)置功能:

下表介紹了本模塊中的一些重要功能:

functionDescription
datetime()datetime 的構(gòu)造函數(shù)
datetime.today()返回當(dāng)前本地日期和時(shí)間
datetime.now()返回當(dāng)前本地日期和時(shí)間
date()以年、月、日為參數(shù),創(chuàng)建相應(yīng)的日期
time()以小時(shí)、分鐘、秒、微秒和tzinfo作為參數(shù),并創(chuàng)建相應(yīng)的日期
date.fromtimestamp()轉(zhuǎn)換秒數(shù)以返回相應(yīng)的日期和時(shí)間
timedelta()它是不同日期或時(shí)間之間的差異(持續(xù)時(shí)間)

使用 datetime 查找日期和時(shí)間

現(xiàn)在,讓我們嘗試實(shí)現(xiàn)這些函數(shù),以使用datetime模塊在 Python 中查找日期和時(shí)間。

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

到此這篇關(guān)于Python中日期和時(shí)間的用法超強(qiáng)總結(jié)的文章就介紹到這了,更多相關(guān)Python日期 時(shí)間用法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • sklearn.metrics 中的f1-score簡(jiǎn)介

    sklearn.metrics 中的f1-score簡(jiǎn)介

    這篇文章主要介紹了sklearn.metrics 中的f1-score簡(jiǎn)介,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-05-05
  • Python缺失值處理方法

    Python缺失值處理方法

    這篇文章主要介紹了Python缺失值處理方法,文章圍繞主題展開(kāi)詳細(xì)內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-05-05
  • python 定時(shí)器,實(shí)現(xiàn)每天凌晨3點(diǎn)執(zhí)行的方法

    python 定時(shí)器,實(shí)現(xiàn)每天凌晨3點(diǎn)執(zhí)行的方法

    今天小編就為大家分享一篇python 定時(shí)器,實(shí)現(xiàn)每天凌晨3點(diǎn)執(zhí)行的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-02-02
  • 使用CodeMirror實(shí)現(xiàn)Python3在線編輯器的示例代碼

    使用CodeMirror實(shí)現(xiàn)Python3在線編輯器的示例代碼

    這篇文章主要介紹了使用CodeMirror實(shí)現(xiàn)Python3在線編輯器的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-01-01
  • Python?VisPy庫(kù)高性能科學(xué)可視化圖形處理用法實(shí)例探究

    Python?VisPy庫(kù)高性能科學(xué)可視化圖形處理用法實(shí)例探究

    VisPy是一個(gè)用于高性能科學(xué)可視化的Python庫(kù),它建立在現(xiàn)代圖形處理單元(GPU)上,旨在提供流暢、交互式的數(shù)據(jù)可視化體驗(yàn),本文將深入探討VisPy的基本概念、核心特性以及實(shí)際應(yīng)用場(chǎng)景,并通過(guò)豐富的示例代碼演示其強(qiáng)大的可視化能力
    2023-12-12
  • 解決Python3.5+OpenCV3.2讀取圖像的問(wèn)題

    解決Python3.5+OpenCV3.2讀取圖像的問(wèn)題

    今天小編就為大家分享一篇解決Python3.5+OpenCV3.2讀取圖像的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-12-12
  • Python格式化字符串f-string簡(jiǎn)介

    Python格式化字符串f-string簡(jiǎn)介

    f-string,亦稱為格式化字符串常量(formatted?string?literals),是Python3.6新引入的一種字符串格式化方法,這篇文章主要介紹了Python格式化字符串f-string概覽,需要的朋友可以參考下
    2022-12-12
  • python 實(shí)現(xiàn)保存最新的三份文件,其余的都刪掉

    python 實(shí)現(xiàn)保存最新的三份文件,其余的都刪掉

    今天小編就為大家分享一篇python 實(shí)現(xiàn)保存最新的三份文件,其余的都刪掉,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-12-12
  • 解決selenium+Headless Chrome實(shí)現(xiàn)不彈出瀏覽器自動(dòng)化登錄的問(wèn)題

    解決selenium+Headless Chrome實(shí)現(xiàn)不彈出瀏覽器自動(dòng)化登錄的問(wèn)題

    這篇文章主要介紹了解決selenium+Headless Chrome實(shí)現(xiàn)不彈出瀏覽器自動(dòng)化登錄的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • 使用Python壓縮和解壓縮zip文件的教程

    使用Python壓縮和解壓縮zip文件的教程

    這篇文章主要介紹了使用Python壓縮和解壓縮zip文件的教程,主要用到了zipfile包,需要的朋友可以參考下
    2015-05-05

最新評(píng)論

屯门区| 南平市| 湘阴县| 萨迦县| 府谷县| 龙井市| 循化| 资源县| 闵行区| 栾城县| 乃东县| 夹江县| 太仓市| 普陀区| 繁昌县| 财经| 平乐县| 左贡县| 黔东| 焉耆| 和龙市| 唐山市| 衡山县| 台中市| 桐梓县| 濮阳县| 南靖县| 全椒县| 泸水县| 吉安县| 和顺县| 土默特右旗| 昌吉市| 五家渠市| 宁远县| 湘阴县| 福贡县| 鲜城| 云梦县| 手游| 天津市|