時間戳與日期格式相互轉換

2021-10-07 22:05:57 字數 3354 閱讀 7089

import time

# 建立乙個時間戳

t =1533880334

# 1. 使用time.localtime將時間戳轉成日期格式

t = time.localtime(t)

# time.struct_time(tm_year=2018, tm_mon=8, tm_mday=10, tm_hour=13, tm_min=52, tm_sec=14, tm_wday=4, tm_yday=222, tm_isdst=0)

# 2. 使用time.strftime將日期轉成日期顯示方式(此處返回結果為文字)

t = time.strftime(

"%y-%m-%d %h:%m:%s"

,t)# '2018-08-10 13:52:14'

將結果這個結果定義成乙個函式,便於日後可能使用

import time

defstamp2time

(t):

try:

t =float

(t)except

:print

('輸入錯入,請輸入乙個時間戳(或float型)'

) t = time.localtime(t)

t = time.strftime(

"%y-%m-%d %h:%m:%s"

,t)return t

**應用:**批量獲取檔案最後一次修改時間

import glob

import time

defstamp2time

(t):

try:

t =float

(t)except

:print

('輸入錯入,請輸入乙個時間戳(或float型)'

) t = time.localtime(t)

t = time.strftime(

"%y-%m-%d %h:%m:%s"

,t)return t

df = pd.dataframe(

)for i in glob.glob(r'd:\pycharmprojects\ipsos_thg\thg_week\result\version_2\*.txt'):

data =

)# file_name time_stamp date

# 0 d:\pycharmprojects\ipsos_thg\thg_week\result\v... 1.534046e+09 2018-08-12 11:59:50

# 0 d:\pycharmprojects\ipsos_thg\thg_week\result\v... 1.534046e+09 2018-08-12 11:59:57

# 0 d:\pycharmprojects\ipsos_thg\thg_week\result\v... 1.534046e+09 2018-08-12 12:00:04

# 0 d:\pycharmprojects\ipsos_thg\thg_week\result\v... 1.534046e+09 2018-08-12 12:00:11

# 0 d:\pycharmprojects\ipsos_thg\thg_week\result\v... 1.534046e+09 2018-08-12 12:00:17

# 0 d:\pycharmprojects\ipsos_thg\thg_week\result\v... 1.534046e+09 2018-08-12 12:00:25

# 0 d:\pycharmprojects\ipsos_thg\thg_week\result\v... 1.534046e+09 2018-08-12 12:00:32

# 0 d:\pycharmprojects\ipsos_thg\thg_week\result\v... 1.534046e+09 2018-08-12 12:00:39

# 0 d:\pycharmprojects\ipsos_thg\thg_week\result\v... 1.534046e+09 2018-08-12 12:00:46

獲取檔案時間的幾種方法

file

= r'd:\pycharmprojects\ipsos_thg\thg_week\result\version_2\week17_m4_0421_0427_4areas_v2.txt'

os.path.getatime(

file

)#輸出最近訪問時間

os.path.getctime(

file

)#輸出檔案建立時間

os.path.getmtime(

file

)#輸出最近修改時間

time.gmtime(os.path.getmtime(

file))

#以struct_time形式輸出最近修改時間

os.path.getsize(

file

)#輸出檔案大小(位元組為單位)

函式

功能os.path.getatime(file)

輸出最近訪問時間

os.path.getctime(file)

輸出檔案建立時間

os.path.getmtime(file)

輸出最近修改時間

time.gmtime(os.path.getmtime(file))

以struct_time形式輸出最近修改時間

os.path.getsize(file)

輸出檔案大小(位元組為單位)

# 1. 首先要確認待轉換的資料是時間格式,通常看起來是時間格式的實際是乙個文字,我們需要用strptime將其轉換為時間格式

t ="2018-08-22 13:14:20"

t = time.strptime(t,

'%y-%m-%d %h:%m:%s'

)# time.struct_time(tm_year=2018, tm_mon=8, tm_mday=22, tm_hour=13, tm_min=14, tm_sec=20, tm_wday=2, tm_yday=234, tm_isdst=-1)

# 2. 呼叫mktime函式將時間轉成時間戳

t = time.mktime(t)

# 1534914860.0

將結果這個結果定義成乙個函式,便於日後可能使用

def time2stamp(t):

t = time.strptime(t,'%y-%m-%d %h:%m:%s')

t = time.mktime(t)

return t

mysql 時間戳與日期格式的相互轉換

1 unix時間戳 轉換為日期用函式 from unixtime sql view plain copy select from unixtime 1156219870 輸出 2006 08 22 12 11 10 2 日期轉換為unix時間戳用函式 unix timestamp sql view ...

js時間戳與日期格式的相互轉換

function timestamptotime timestamp timestamptotime 1403058804 console.log timestamptotime 1403058804 2014 06 18 10 33 24注意 如果是unix時間戳記得乘以1000。比如 php函式...

js時間戳與日期格式之間相互轉換

將時間戳轉換成日期格式 簡單的一句 var date new date 時間戳 獲取乙個時間物件 date.getfullyear 獲取完整的年份 4位,1970 date.getmonth 獲取月份 0 11,0代表1月,用的時候記得加上1 date.getdate 獲取日 1 31 date.g...