python将datetime转为时间戳,将python datetime转换为时间戳(以毫秒为单位)

How to convert a human readable time with the format 20.12.2016 09:38:42,76 to Unix timestamps in milliseconds? I found a lot of similar questions, but didn't found this specific question/answer.

解决方案

In Python 3 this can be done in 2 steps:

Convert timestring to datetime object

Multiply the timestamp of the datetime object by 1000 to convert it to milliseconds.

For example like this:

from datetime import datetime

dt_obj = datetime.strptime('20.12.2016 09:38:42,76',

'%d.%m.%Y %H:%M:%S,%f')

millisec = dt_obj.timestamp() * 1000

print(millisec)

Output:

1482223122760.0

strptime accepts your timestring and a format string as input. The timestring (first argument) specifies what you actually want to convert to a datetime object. The format string (second argument) specifies the actual format of the string that you have passed.

Here is the explanation of the format specifiers from the official documentation:

%d - Day of the month as a zero-padded decimal number.

%m - Month as a zero-padded decimal number.

%Y - Year with century as a decimal number

%H - Hour (24-hour clock) as a zero-padded decimal number.

%M - Minute as a zero-padded decimal number.

%S - Second as a zero-padded decimal number.

%f - Microsecond as a decimal number, zero-padded on the left.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值