當前位置:首頁 » 編程語言 » python時間戳轉datetime

python時間戳轉datetime

發布時間: 2022-03-15 11:48:51

⑴ 用python將某一列時間戳轉為時間日期格式

應該是不能直接全部替換,只能一行行讀取,獲取時間戳,然後轉換:
time_stamp = 1494579361
time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time_stamp))

⑵ 怎麼樣在Python中把時間戳改成時間

importtime
x=time.localtime(1375963485)
a=time.strftime('%Y-%m-%d%H:%M:%S',x)
printa

⑶ python使用pyHook.HookManager()返回來的event中,event.Time怎麼轉換成為datetime形式

我覺得不是時間戳(或者說,不是通常意義下的時間戳)。

這里的event可能是KeyboardEvent或 MouseEvent(視鉤子類型而定)。而這兩個類又都是HookEvent的子類。

HookEvent有如下幾個成員:

Message: Keyboard or mouse event message

Time: Seconds since the epoch when the even current

Window: Window handle of the foreground window at the time of the event

WindowName: Name of the foreground window at the time of the event

這里,對Time成員的描述是:

Seconds since the epoch when the even current


這里的epoch很有意思。

一般上,我們在使用Python中的time模塊,或者C標准庫中的time.h時,認為epoch是(摘自Python2.7 time模塊的文檔):

The epoch is the point where the time starts. On January 1st of that year, at 0 hours, the 「time since the epoch」 is zero. For Unix, the epoch is 1970.

但是,這里的epoch卻不是。看下面一段改編自pyhook官網的小例子:

#-*-coding:utf-8-*-
importpythoncom,pyHook
importwin32api
importtime

defOnKeyboardEvent(event):
printevent.Time
#.
#
#.OnJanuary1stofthatyear,
#at0hours,the「timesincetheepoch」iszero.ForUnix,theepochis1970.
printtime.time()
#
printwin32api.GetTickCount()

print'MessageName:',event.MessageName
print'Message:',event.Message
print'Time:',time.ctime(time.time())
print'Window:',event.Window
print'WindowName:',event.WindowName
print'Ascii:',event.Ascii,chr(event.Ascii)
print'Key:',event.Key
print'KeyID:',event.KeyID
print'ScanCode:',event.ScanCode
print'Extended:',event.Extended
print'Injected:',event.Injected
print'Alt',event.Alt
print'Transition',event.Transition
print'---'

#
returnTrue

#createahookmanager
hm=pyHook.HookManager()
#watchforallkeyboardevents
hm.KeyDown=OnKeyboardEvent
#setthehook
hm.HookKeyboard()
#waitforever
pythoncom.PumpMessages()

其中:

print event.Time

print time.time()

print win32api.GetTickCount()

我發現,event.Time和GetTickCount返回的值是一樣的。而GetTickCount的含義是:

Returns the number of milliseconds since windows started

也就是,從本次開機到GetTickCount調用時經過的毫秒數。

所以,不能依賴event.Time來獲取時間了。而time模塊就派上用場了。比如:

print'Time:',time.ctime(time.time())

就可以列印消息發生時的年月日時分秒了。

⑷ 怎樣用spss中的python語法將V2列的時間戳轉為時間(年月日時分秒)

# -*- coding: utf-8 -*-
import math

#實現整除運算
def div(x,y):
return int(round(x)/round(y))
#return $cal2('x','x',p_div,x,y);
def DF2DHMS(F):
df = F
day = math.floor(df)
hour = math.floor((df-day)*24)
minute = math.floor((df-day-hour/24)*1440)
sec = (df-day-hour/24-minute/1440)*86400
return [day,hour,minute,sec]
MJD=17366.62152773142
DJMIN = -68569.5
DJMAX = 1e9

DJ1 = 2400000.5
DJ2 = MJD
DJ = DJ1 + DJ2
D1 =''
D2 =''
J =''
JD =''
if ( DJ < DJMIN or DJ > DJMAX ):

J = -1
print u'無效的日期: '+MJD
print J
else:
J = 0
if ( DJ1 >= DJ2 ):
D1 = DJ1
D2 = DJ2
else:
D1 = DJ2
D2 = DJ1
D2 = D2 - 0.5
F1 = D1%1.0
F2 = D2%1.0
F = (F1+F2) % 1.0
if ( F < 0 ) :F = F + 1.0
D = round(D1-F1) + round(D2-F2) + round(F1+F2-F)
JD = round(D) + 1
L = JD + 68569
N = div( 4*L , 146097)
L = L - div(( 146097*N + 3 ) , 4)
I = div( 4000 * (L+1) , 1461001)
L = L - div( 1461*I , 4) + 31
K = div( 80*L , 2447)
ID = L - div( 2447*K , 80)
L = div(K , 11)

IM = K + 2 - 12*L
IY = 100 * ( N-49 ) + I + L
FD = DF2DHMS(F)
print MJD,'對應日期為',[IY,IM,int(ID),int(FD[1]),int(FD[2]),FD[3]]

-------

>>>
17366.6215277 對應日期為 [1906, 6, 5, 14, 54, 59.99599456786802]
>>>
http://www.scicalweb.com/html/online-calculate/609.html

⑸ python 如何循環處理列表中的時間戳

你好!

希望對你有幫助!

⑹ python能把正常時間轉化成毫秒級別的時間戳嗎

可以,需要乘以1000,比如

importtime
printtime.time()*1000#這個就是毫秒級別的時間戳,不乘以1000就是秒級時間戳

又如正常時間轉換為時間戳,下面的例子需要你安裝時間擴展arrow,安裝方法很簡單

pip install arrow,這都是廢話了,下面上例子

importarrow
datetime="2016-12-2222:30:50+08:00"
printarrow.get(datetime).timestamp#這個是秒級別的時間戳,乘以1000就是毫秒級別的時間戳了

⑺ python問題,字元串的時間可以直接轉換為時間戳嗎

ts = '2020-04-01 13:43:36'

dt = datetime.datetime.strptime(ts, '%Y-%m-%d %H:%M:%S')
tstamp = dt.timestamp()

⑻ python datetime處理時間

python時間處理方法datetime(),下面就舉幾個代碼案例進行說明,代碼如下:

#-*-coding:utf-8-*-
#運行環境:Python3.4
#datetime類
#datetime是date與time的結合體,包括date與time的所有信息。
#它的構造函數如下:
#datetime.datetime(year,month,day[,hour[,minute[,second[,microsecond[,tzinfo]]]]])
#各參數的含義與date、time的構造函數中的一樣,要注意參數值的范圍。

#1.datetime類定義的類屬性與方法:
#datetime.min、datetime.max:datetime所能表示的最小值與最大值;
#print:datetime.max:9999-12-3123:59:59.999999
#print:datetime.min:0001-01-0100:00:00
fromdatetimeimport*
importtime
print('datetime.max:'+str(datetime.max))
print('datetime.min:'+str(datetime.min))
#datetime.resolution:datetime最小單位;
#print:datetime.resolution:0:00:00.000001
print('datetime.resolution:'+str(datetime.resolution))
#datetime.today():返回一個表示當前本地時間的datetime對象;
#print:today():2012-09-1219:37:50.721000
print('today():'+str(datetime.today()))
#datetime.now([tz]):返回一個表示當前本地時間的datetime對象,如果提供了參數tz,則獲取tz參數所指時區的本地時間;
#print:now():2012-09-1219:37:50.738000
print('now():'+str(datetime.now()))
#datetime.utcnow():返回一個當前utc時間的datetime對象;
#print:2012-09-1211:37:50.739000
print('utcnow():'+str(datetime.utcnow()))
#datetime.fromtimestamp(timestamp[,tz]):根據時間戮創建一個datetime對象,參數tz指定時區信息;
#print:fromtimestamp(tmstmp):2012-09-1219:37:50.741000
print('fromtimestamp(tmstmp):'+str(datetime.fromtimestamp(time.time())))
#datetime.utcfromtimestamp(timestamp):根據時間戮創建一個datetime對象;
#print:utcfromtimestamp(tmstmp):2012-09-1211:37:50.742000
print('utcfromtimestamp(tmstmp):'+str(datetime.utcfromtimestamp(time.time())))
#datetime.combine(date,time):根據date和time,創建一個datetime對象;
#print:datetime.combine(date,time):2012-09-1219:46:05
d=date(2012,9,12)
fromdatetimeimport*
t=time(19,46,5)
print('datetime.combine(date,time):'+str(datetime.combine(d,t)))
#datetime.strptime(date_string,format):將格式字元串轉換為datetime對象;
#print:2007-03-0421:08:12
print(datetime.strptime("2007-03-0421:08:12","%Y-%m-%d%H:%M:%S"))

#2.datetime類提供的實例方法與屬性
dt=datetime.strptime("2012-09-1221:08:12","%Y-%m-%d%H:%M:%S")
#print:2012912218120None
print(dt.year)
print(dt.month)
print(dt.day)
print(dt.hour)
print(dt.minute)
print(dt.second)
print(dt.microsecond)
print(dt.tzinfo)
print(dt.date())
print(dt.time())
print(dt.replace(year=2013))
print(dt.timetuple())
print(dt.utctimetuple())
print(dt.toordinal())
print(dt.weekday())
print(dt.isocalendar())
#printdt.isoformat([sep])
#datetime.ctime():返回一個日期時間的C格式字元串,等效於time.ctime(time.mktime(dt.timetuple()));

#3.格式字元串
#datetime.strftime(format)
#%a星期的簡寫。如星期三為Web
#%A星期的全寫。如星期三為Wednesday
#%b月份的簡寫。如4月份為Apr
#%B月份的全寫。如4月份為April
#%c:日期時間的字元串表示。(如:04/07/1010:43:39)
#%d:日在這個月中的天數(是這個月的第幾天)
#%f:微秒(范圍[0,999999])
#%H:小時(24小時制,[0,23])
#%I:小時(12小時制,[0,11])
#%j:日在年中的天數[001,366](是當年的第幾天)
#%m:月份([01,12])
#%M:分鍾([00,59])
#%p:AM或者PM
#%S:秒(范圍為[00,61],為什麼不是[00,59],參考python手冊~_~)
#%U:周在當年的周數當年的第幾周),星期天作為周的第一天
#%w:今天在這周的天數,范圍為[0,6],6表示星期天
#%W:周在當年的周數(是當年的第幾周),星期一作為周的第一天
#%x:日期字元串(如:04/07/10)
#%X:時間字元串(如:10:43:39)
#%y:2個數字表示的年份
#%Y:4個數字表示的年份
#%z:與utc時間的間隔(如果是本地時間,返回空字元串)
#%Z:時區名稱(如果是本地時間,返回空字元串)
#%%:%%=>%

dt=datetime.now()
#print:(%Y-%m-%d%H:%M:%S%f):2012-09-1223:04:27145000
print('(%Y-%m-%d%H:%M:%S%f):'+str(dt.strftime('%Y-%m-%d%H:%M:%S%f')))
#print:(%Y-%m-%d%H:%M:%S%p):12-09-1211:04:27PM
print('(%Y-%m-%d%H:%M:%S%p):'+str(dt.strftime('%y-%m-%d%I:%M:%S%p')))
#print:%a:Wed
print('%%a:%s'%dt.strftime('%a'))
#print:%A:Wednesday
print('%%A:%s'%dt.strftime('%A'))
#print:%b:Sep
print('%%b:%s'%dt.strftime('%b'))
#print:%B:September
print('%%B:%s'%dt.strftime('%B'))
#print:日期時間%c:09/12/1223:04:27
print('日期時間%%c:%s'%dt.strftime('%c'))
#print:日期%x:09/12/12
print('日期%%x:%s'%dt.strftime('%x'))
#print:時間%X:23:04:27
print('時間%%X:%s'%dt.strftime('%X'))
#print:今天是這周的第3天
print('今天是這周的第%s天'%dt.strftime('%w'))
#print:今天是今年的第256天
print('今天是今年的第%s天'%dt.strftime('%j'))
#print:今周是今年的第37周
print('今周是今年的第%s周'%dt.strftime('%U'))

上面代碼案例運行結果如下:

atetime.max:9999-12-3123:59:59.999999

datetime.min:0001-01-0100:00:00

datetime.resolution:0:00:00.000001

today():2014-05-0415:58:18.141186

now():2014-05-0415:58:18.193146

utcnow():2014-05-0407:58:18.243958

fromtimestamp(tmstmp):2014-05-0415:58:18.291558

utcfromtimestamp(tmstmp):2014-05-0407:58:18.342550

datetime.combine(date,time):2012-09-1219:46:05

2007-03-0421:08:12

2012

9

12

21

8

12

0

None

2012-09-12

21:08:12

2013-09-1221:08:12

time.struct_time(tm_year=2012,tm_mon=9,tm_mday=12,tm_hour=21,tm_min=8,tm_sec=12,tm_wday=2,tm_yday=256,tm_isdst=-1)

time.struct_time(tm_year=2012,tm_mon=9,tm_mday=12,tm_hour=21,tm_min=8,tm_sec=12,tm_wday=2,tm_yday=256,tm_isdst=0)

734758

2

(2012,37,3)

(%Y-%m-%d%H:%M:%S%f):2014-05-0415:58:19326295

(%Y-%m-%d%H:%M:%S%p):14-05-0403:58:19PM

%a:Sun

%A:Sunday

%b:May

%B:May

日期時間%c:SunMay415:58:192014

日期%x:05/04/14

時間%X:15:58:19

今天是這周的第0天

今天是今年的第124天

今周是今年的第18周

⑼ python中,有個字元串形式的時間戳,如何轉換為日期呢

用datetime.datetime.strptime()函數;
a=datetime.datetime.strptime('2018-1-8 10:10:10','%Y-%m-%d %H:%M:%S')
輸出的a就是時間格式

⑽ python中怎麼把datetime類型轉換成timestamp

Python3.6.4(v3.6.4:d48ecebad5,Dec182017,21:07:28)
[GCC4.2.1(AppleInc.build5666)(dot3)]ondarwin
Type"help","right","credits"or"license"formoreinformation.
>>>importtime
>>>fromdatetimeimportdatetime
>>>now=datetime.now()
>>>timestamp=int(time.mktime(now.timetuple()))
>>>timestamp
1520493295
>>>timestamp_microsecond=float('{}{:06}'.format(timestamp,now.microsecond))/1000000
>>>timestamp_microsecond
1520493295.337066

熱點內容
寶可夢我的世界自創伺服器 發布:2025-07-22 18:09:31 瀏覽:226
男人的圖片密碼多少 發布:2025-07-22 18:07:45 瀏覽:367
ftp直接訪問nas 發布:2025-07-22 18:00:53 瀏覽:233
java的對象的屬性值 發布:2025-07-22 17:58:59 瀏覽:745
黑魂的伺服器是電腦和主機共用嗎 發布:2025-07-22 17:56:35 瀏覽:630
linux看時間 發布:2025-07-22 17:55:37 瀏覽:102
雲更新賬號已綁定其他伺服器 發布:2025-07-22 17:36:56 瀏覽:60
為什麼垃圾安卓老是卡 發布:2025-07-22 17:36:45 瀏覽:490
長沙cnc編程招聘信息 發布:2025-07-22 17:36:12 瀏覽:749
ubuntu顯示文件夾 發布:2025-07-22 17:35:50 瀏覽:620