Unix時間戳轉換 python

2021-08-19 22:18:29 字數 1980 閱讀 9397

# -*- coding: utf-8 -*-

importtime

deftimestamp_datetime(value):

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

# value為傳入的值為時間戳(整形),如:1332888820

value=time.localtime(value)

## 經過localtime轉換後變成

## time.struct_time(tm_year=2012, tm_mon=3, tm_mday=28, tm_hour=6, tm_min=53, tm_sec=40, tm_wday=2, tm_yday=88, tm_isdst=0)

# 最後再經過strftime函式轉換為正常日期格式。

dt=time.strftime(format, value)

returndt

defdatetime_timestamp(dt):

#dt為字串

#中間過程,一般都需要將字串轉化為時間陣列

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

## time.struct_time(tm_year=2012, tm_mon=3, tm_mday=28, tm_hour=6, tm_min=53, tm_sec=40, tm_wday=2, tm_yday=88, tm_isdst=-1)

#將"2012-03-28 06:53:40"轉化為時間戳

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

returnint(s)

if__name__=='__main__':

d=datetime_timestamp('2012-03-28 06:53:40')

printd

s=timestamp_datetime(1332888820)

prints

Unix時間戳轉換 python

coding utf 8 import time deftimestamp datetime value format y m d h m s value為傳入的值為時間戳 整形 如 1332888820 value time.localtime value 經過localtime轉換後變成 tim...

unix時間戳和普通時間戳 轉換

unix時間戳是從1970年1月1日 utc gmt的午夜 開始所經過的秒數,不考慮閏秒,以秒為單位 new date gettime 獲得的是以毫秒為單位的 js中獲取unix時間戳的方式 math.round new date gettime 1000 gettime 返回數值的單位是毫秒 un...

Oracle時間與Unix時間戳的轉換

關鍵字 oracle data unix unix時間戳記是從 1970 01 01 00 00 00 gmt開始的秒數,表現為整數型。oracle中的時間是date型,以下函式提供了兩種時間轉換的oracle函式 1 從unix時間戳記轉換為oracle時間 create or replace f...