搜尋
首頁後端開發Python教學python時間處理詳解

1.獲取當前時間的兩種方法:

import datetime,time
now = time.strftime("%Y-%m-%d %H:%M:%S")
print now
now = datetime.datetime.now()
print now

2.獲取上個月最後一天的日期(本月的第一天減去1天)

last = datetime.date(datetime.date.today().year,datetime.date.today().month,1)-datetime.timedelta(1)
print last

3.獲取時間差(時間差單位為秒,常用於計算程式運行的時間)

starttime = datetime.datetime.now()
#long running
endtime = datetime.datetime.now()
print (endtime - starttime).seconds

4.計算當前時間向後10個小時的時間

d1 = datetime.datetime.now()
d3 = d1 + datetime.timedelta(hours=10)
d3.ctime()

其本上常用的類別有:datetime和timedelta兩個。它們之間可以相互加減。每個類別都有一些方法和屬性可以查看具體的值,如datetime可以查看:天數(day),小時數(hour),星期幾(weekday())等;timedelta可以查看:天數(days),秒數(seconds)等。

5.python中時間日期格式化符號:

%y 兩位數的年份表示(00-99)

%Y 四位數的年份表示(000-9999)

%m 月份(01- 12)

%d 月內中的一天(0-31)

%H 24小時制小時數(0-23)

%I 12小時制小時數(01-12) 

%M 分鐘數(00=59)

%S 秒(00-59)


%a 本地簡化星期名稱

%A 本地完整星期名稱

%b 本地簡化的月份名稱

%A 本地完整星期名稱

%b 本地簡化的月份名稱

%A 本地完整星期名稱

%b 本地簡化的月份名稱

%A 本地完整星期名稱

%b 本地簡化的月份名稱

月份名稱

%j 年內的一天(001-366)

%p 本地A.M.或P.M.的等價符

%U 一年中的星期數(000 -53)星期日為星期的開始

%w 星期(0-6),星期天為星期的開始

%W 一年中的星期數(00-53)星期一為星期的開始

%x 本地對應的日期表示

%X 本地對應的時間表示

%Z 目前時區的名稱

%%% %號本身

附上範例程式碼:

程式碼Code highlighting produced by /www.CodeHighlighter.com/-->#-*-coding:utf-8-*-   


import datetime, calendar   

def getYesterday():   
    today=datetime.date.today()   
    oneday=datetime.timedelta(days=1)   
    yesterday=today-oneday   
   return yesterday
def getToday():   
    return datetime.date.today()
🎜#獲取給定參數的前幾天的日期,返回一個一個🎜
陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
Python:編譯器還是解釋器?Python:編譯器還是解釋器?May 13, 2025 am 12:10 AM

Python是解釋型語言,但也包含編譯過程。 1)Python代碼先編譯成字節碼。 2)字節碼由Python虛擬機解釋執行。 3)這種混合機制使Python既靈活又高效,但執行速度不如完全編譯型語言。

python用於循環與循環時:何時使用哪個?python用於循環與循環時:何時使用哪個?May 13, 2025 am 12:07 AM

UseeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.forloopsareIdealForkNownsences,而WhileLeleLeleLeleLeleLoopSituationSituationsItuationsItuationSuationSituationswithUndEtermentersitations。

Python循環:最常見的錯誤Python循環:最常見的錯誤May 13, 2025 am 12:07 AM

pythonloopscanleadtoerrorslikeinfiniteloops,modifyingListsDuringteritation,逐個偏置,零indexingissues,andnestedloopineflinefficiencies

對於循環和python中的循環時:每個循環的優點是什麼?對於循環和python中的循環時:每個循環的優點是什麼?May 13, 2025 am 12:01 AM

forloopsareadvantageousforknowniterations and sequests,供應模擬性和可讀性;而LileLoopSareIdealFordyNamicConcitionSandunknowniterations,提供ControloperRoverTermination.1)forloopsareperfectForeTectForeTerToratingOrtratingRiteratingOrtratingRitterlistlistslists,callings conspass,calplace,cal,ofstrings ofstrings,orstrings,orstrings,orstrings ofcces

Python:深入研究彙編和解釋Python:深入研究彙編和解釋May 12, 2025 am 12:14 AM

pythonisehybridmodeLofCompilation和interpretation:1)thepythoninterpretercompilesourcecececodeintoplatform- interpententbybytecode.2)thepythonvirtualmachine(pvm)thenexecutecutestestestestestesthisbytecode,ballancingEaseofuseEfuseWithPerformance。

Python是一種解釋或編譯語言,為什麼重要?Python是一種解釋或編譯語言,為什麼重要?May 12, 2025 am 12:09 AM

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允許fordingfordforderynamictynamictymictymictymictyandrapiddefupment,儘管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

對於python中的循環時循環與循環:解釋了關鍵差異對於python中的循環時循環與循環:解釋了關鍵差異May 12, 2025 am 12:08 AM

在您的知識之際,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations則youneedtoloopuntilaconditionismet

循環時:實用指南循環時:實用指南May 12, 2025 am 12:07 AM

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境