python 時間戳轉各時區時間

2021-10-08 23:18:32 字數 1079 閱讀 7640

**

import pytz

from datetime import datetime

import time

# 各時區例項

utc = pytz.utc

geijing = pytz.timezone(

"asia/shanghai"

)pst = pytz.timezone(

"us/pacific"

)tokyo = pytz.timezone(

"asia/tokyo"

)#時間戳

loc_timestamp = time.time(

)print

("時間戳:%s"

% loc_timestamp)

# 轉utc時間 datetime.datetime 型別

utc_date = datetime.utcfromtimestamp(loc_timestamp)

print

(% utc_date)

# 轉utc當地 標識的時間

utc_loc_time = utc.localize(utc_date)

print

("utc時間標記:%s"

% utc_loc_time)

fmt =

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

# 轉pst時間

pst_time = utc_loc_time.astimezone(pst)

print

("pst時間標記:%s"

% pst_time)

print

(pst_time.strftime(fmt)

)# 轉北京時間

beijing_time = utc_loc_time.astimezone(geijing)

print

("北京時間標記:%s"

%beijing_time)

print

(beijing_time.strftime(fmt)

)

結果

時間轉時間戳

把現在時間轉成timestamp datetime gtm new datetime 1970,1,1 宣告乙個gtm時間出來 datetime utc datetime.utcnow.addhours 8 宣告乙個目前的時間 int timestamp convert.toint32 timesp...

時間轉時間戳

把現在時間轉成timestamp datetime gtm new datetime 1970,1,1 宣告乙個gtm時間出來 datetime utc datetime.utcnow.addhours 8 宣告乙個目前的時間 int timestamp convert.toint32 timesp...

JS時間轉時間戳,時間戳轉時間。時間顯示模式。

時間轉為時間戳 function date2timestamp datetime 時間戳轉時間 function timestamp2date timestamp,mode replace 年 月 g,replace 日 g,replace 上午 g,replace 下午 g,var date ar...