時間戳是指格林威治時間自1970年1月1日(00:00:00 GMT)至目前時間的總秒數。它也被稱為Unix時間戳(Unix Timestamp)。
百度百科對時間戳的定義是,一個能表示一份資料在某個特定時間之前已經存在的、完整的、可驗證的資料,通常是一個字元序列,唯一地標識某一刻的時間。
在python中,時間戳記是一串數字,當我們要把它轉換成有一定格式的時間資料如'2018-08-08 11:11:11' 時,需要用到time模組。同樣,時間資料也可以轉換為時間戳記。
具體操作有以下幾種:
將時間轉換為時間戳
#重新格式化時間
時間戳轉換為時間
取得目前時間及將其轉換成時間戳記(實際需求)
將時間轉換成時間戳
#將時間資料2018-08-08 11:11:11轉換成時間戳,
具體的操作過程為:
利用strptime()函數將時間轉換成時間數組! !
利用mktime()函數將時間數組轉換成時間戳記!!
import time dt = '2018-08-08 11:11:11' # 将时间转换成时间数组 timeArray = time.strptime(dt, "%Y-%m-%d %H:%M:%S") print(timeArray) #time.struct_time(tm_year=2018, tm_mon=8, tm_mday=8, tm_hour=11, tm_min=11,tm_sec=11,tm_wday=2,tm_yday=220,tm_isdst=-1) print(timeArray[0]) # 2018 # 将时间数组转换成时间戳 timestamp = time.mktime(timeArray) print(timestamp) # 1533697871.0
重新格式化時間
重新格式化時間需要以下的兩個步驟:
利用strptime()函數將時間轉換成時間數組
使用strftime()函數重新格式化時間
import time dt = '2018-08-08 11:11:11' # 利用strptime()函数将时间转换成时间数组 timeArray = time.strptime(dt, '%Y-%m-%d %H:%M:%S') # 利用strftime()函数重新格式化时间 dt_new = time.strftime('%Y-%m-%d - %H:%M:%S',timeArray) print(dt_new) # 2018-08-08 - 11:11:11
將時間戳轉換成時間
在時間戳記轉換成時間中,首先需要將時間戳轉換成localtime,再轉換成時間的具體格式:
利用localtime()函數將時間戳轉換成localtime的格式
利用strftime()函數重新格式化時間
import time timestamp = 1533697871.0 # 利用localtime()函数将时间戳转化成时间数组 localtime = time.localtime(timestamp) print(localtime) #time.struct_time(tm_year=2018, tm_mon=8, tm_mday=8,tm_hour=11,tm_min=11,tm_sec=11, tm_wday=2, tm_yday=220, tm_isdst=0) # 利用strftime()函数重新格式化时间 dt = time.strftime('%Y:%m:%d %H:%M:%S',localtime) print(dt) # 2018:08:08 11:11:11
以指定的格式取得目前時間
基本步驟:
利用time()取得目前時間,
再利用localtime()函數轉換為localtime(本地)時間數組,
最後利用strftime()函數重新格式化時間。
import time # 获取当前时间 current_time = int(time.time()) print(current_time) # 1537873862 # 转换为localtime localtime = time.localtime(current_time) # 利用strftime()函数重新格式化时间 dt = time.strftime('%Y:%m:%d %H:%M:%S', localtime) print(dt) # 返回当前时间:2019:07:19 4:14:34
以上是python時間戳記是什麼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

pythonlistsareimplementedasdynamicarrays,notlinkedlists.1)他們areStoredIncoNtiguulMemoryBlocks,mayrequireRealLealLocationWhenAppendingItems,EmpactingPerformance.2)LinkesedlistSwoldOfferefeRefeRefeRefeRefficeInsertions/DeletionsButslowerIndexeDexedAccess,Lestpypytypypytypypytypy

pythonoffersFourmainMethodStoreMoveElement Fromalist:1)刪除(值)emovesthefirstoccurrenceofavalue,2)pop(index)emovesanderturnsanelementataSpecifiedIndex,3)delstatementremoveselemsbybybyselementbybyindexorslicebybyindexorslice,and 4)

toresolvea“ dermissionded”錯誤Whenrunningascript,跟隨台詞:1)CheckAndAdjustTheScript'Spermissions ofchmod xmyscript.shtomakeitexecutable.2)nesureThEseRethEserethescriptistriptocriptibationalocatiforecationAdirectorywherewhereyOuhaveWritePerMissionsyOuhaveWritePermissionsyYouHaveWritePermissions,susteSyAsyOURHomeRecretectory。

ArraysarecrucialinPythonimageprocessingastheyenableefficientmanipulationandanalysisofimagedata.1)ImagesareconvertedtoNumPyarrays,withgrayscaleimagesas2Darraysandcolorimagesas3Darrays.2)Arraysallowforvectorizedoperations,enablingfastadjustmentslikebri

ArraySaresificatificallyfasterthanlistsForoperationsBenefiting fromDirectMemoryAcccccccCesandFixed-Sizestructures.1)conscessingElements:arraysprovideconstant-timeaccessduetocontoconcotigunmorystorage.2)iteration:araysleveragececacelocality.3)

ArraySareBetterForlement-WiseOperationsDuetofasterAccessCessCessCessCessCessCessCessAndOptimizedImplementations.1)ArrayshaveContiguucuulmemoryfordirectAccesscess.2)列出sareflexible butslible butslowerduetynemicizing.3)

在NumPy中进行整个数组的数学运算可以通过向量化操作高效实现。1)使用简单运算符如加法(arr 2)可对数组进行运算。2)NumPy使用C语言底层库,提升了运算速度。3)可以进行乘法、除法、指数等复杂运算。4)需注意广播操作,确保数组形状兼容。5)使用NumPy函数如np.sum()能显著提高性能。

在Python中,向列表插入元素有兩種主要方法:1)使用insert(index,value)方法,可以在指定索引處插入元素,但在大列表開頭插入效率低;2)使用append(value)方法,在列表末尾添加元素,效率高。對於大列表,建議使用append()或考慮使用deque或NumPy數組來優化性能。


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

WebStorm Mac版
好用的JavaScript開發工具

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Atom編輯器mac版下載
最受歡迎的的開源編輯器