python時間字串與時間戳的轉換

2021-10-06 05:49:52 字數 4294 閱讀 3777

import datetime

import time

print

(datetime.datetime.now(

).year)

# 返回當前時間年

print

(datetime.datetime.now(

).month)

# 返回當前時間月

print

(datetime.datetime.now(

).day)

# 返回當前時間日

time_float = time.time(

)# 返回當前的浮點數時間戳

print

(time_float)

time_int =

int(time.time())

# 返回當前的時間戳(整數)

print

(time_int)

time_int_millisecond =

int(

round

(time.time()*

1000))

# 返回當前的毫秒級的時間戳(整數)

print

(time_int_millisecond)

# 毫秒級和秒級之間差1000進製,轉換乘除1000即可

time_str = time.ctime(

)# 返回當前的時間字串

print

(time_str)

time_array = time.gmtime(

)# 返回當前的時間陣列(計算機可處理的時間)

print

(time_array)

結果:

2020525

1590383831.2612488

1590383831

1590383831261

mon may 25 13:17:11 2020

time.struct_time(tm_year=2020, tm_mon=5, tm_mday=25, tm_hour=5, tm_min=17, tm_sec=11, tm_wday=0, tm_yday=146, tm_isdst=0)

字串轉時間戳

python time strptime() 函式根據指定的格式把乙個時間字串解析為時間元組,time.strptime(string[, format]),第乙個引數就是時間字串; 第二個就是格式化字串。

python time mktime() 函式執行與gmtime(), localtime()相反的操作,它接收struct_time物件作為引數,返回用秒級來表示時間的浮點數(時間戳)。如果輸入的值不是乙個合法的時間,將觸發 overflowerror 或 valueerror。

import time

time_str =

"2020-05-17 14:55:10"

# python time strptime() 函式根據指定的格式把乙個時間字串解析為時間元組

# time.strptime(string[, format])

# 第乙個引數就是時間字串; 第二個就是格式化字串

time_array = time.strptime(time_str,

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

)print

('返回陣列:'

,time_array)

# 轉換為時間戳

# python time mktime() 函式執行與gmtime(), localtime()相反的操作,它接收struct_time物件作為引數,返回用秒數來表示時間的浮點數。

# 如果輸入的值不是乙個合法的時間,將觸發 overflowerror 或 valueerror。

time_float = time.mktime(time_array)

# 可以轉化為int型別

print

(time_float)

結果:

返回陣列: time.struct_time(tm_year=2020, tm_mon=5, tm_mday=17, tm_hour=14, tm_min=55, tm_sec=10, tm_wday=6, tm_yday=138, tm_isdst=-1)

1589698510.0

時間戳轉字串

python time localtime() 函式類似gmtime(),作用是格式化時間戳為本地的時間陣列。time.localtime([ sec ]),如果sec引數未輸入,則以當前時間為轉換標準。

import time

import datetime

time_int =

1589698510

# 使用localtime()轉換為時間陣列

time_array = time.localtime(time_int)

print

(time_array)

# 再格式化自己想要的格式

# 本地時間

local_time = time.strftime(

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

, time_array)

print

(local_time)

# utc時間

datetime_array = datetime.datetime.utcfromtimestamp(time_int)

utc_time = datetime_array.strftime(

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

)print

(utc_time)

結果:

time.struct_time(tm_year=2020, tm_mon=5, tm_mday=17, tm_hour=14, tm_min=55, tm_sec=10, tm_wday=6, tm_yday=138, tm_isdst=0)

2020-05-17 14:55:10

2020-05-17 06:55:10

改變時間字串輸出形式(「2020-05-17 14:55:10」 改為 「2020/05/1714:55:10」)

import time

time_str =

"2020-05-17 14:55:10"

print

(time_str)

# 先轉換為時間陣列

time_array = time.strptime(time_str,

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

)print

(time_array)

#"2020-05-17 14:55:10" 改為 "2020/05/1714:55:10"

other_time = time.strftime(

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

, time_array)

print

(other_time)

結果:

2020-05-17 14:55:10

time.struct_time(tm_year=2020, tm_mon=5, tm_mday=17, tm_hour=14, tm_min=55, tm_sec=10, tm_wday=6, tm_yday=138, tm_isdst=-1)

2020/05/1714:55:10

獲取目標形式的幾天前時間

import time

import datetime

# 先獲得時間陣列格式的日期

three_day_ago =

(datetime.datetime.now(

)- datetime.timedelta(days=3)

)#獲取三天前的時間

print

(three_day_ago)

# 轉換為時間戳

time_int =

int(time.mktime(three_day_ago.timetuple())

)print

(time_int)

# 轉換為其他形式的字串

other_time = three_day_ago.strftime(

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

)print

(other_time)

結果:

2020-05-14 16:26:42.889400

1589444802

2020/14/0516:26:42

Python 時間戳 字串 時間 轉換

平時對於時間的處理經常使用python的time和datetime模組,但是用來多次還是對其中的時間戳,字串和時間轉換應用的不太熟練,時間長了不使用就理不清楚,為此整理成文。時間戳,時間,字串之間的關係整理如下圖 時間戳 time.time 返回當前時間戳 seconds time.time tim...

python 字串時間轉成時間戳

示例一import time t 2020 10 31 12 44 27 將字串形式的時間轉換為時間元組 t time.strptime t,y m d h m s 將時間元組轉換為時間戳 t time.mktime t 1604119467.0 print t 示例二import time t m...

python 時間戳 時間字串轉換

使用time和datetime包進行轉換。環境python2.7.13。gmt 格林威治時間,bjt 北京時間。時間戳轉為時間字串 coding utf 8 時間戳 gmt 轉化為字串 bjt import time import datetime timestamp 1522165684 時間戳是...