当前位置:首页 » 编程语言 » 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 12:43:51 浏览:561
文件保存网盘自己会解压吗 发布:2025-07-22 12:37:56 浏览:100
多大孩子适合学编程 发布:2025-07-22 12:24:05 浏览:88
代理服务器占ip 发布:2025-07-22 12:24:01 浏览:579
java全局变量 发布:2025-07-22 12:19:14 浏览:151
osgi源码 发布:2025-07-22 12:12:38 浏览:29
yarphp 发布:2025-07-22 12:04:32 浏览:262
暗区突围辅助脚本 发布:2025-07-22 12:04:29 浏览:757
js操作数据库 发布:2025-07-22 11:58:07 浏览:525
办公及生活配置有哪些 发布:2025-07-22 11:55:05 浏览:687