以毫秒为单位将python日期时间转换为时间戳

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41635547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 01:20:28  来源:igfitidea点击:

Convert python datetime to timestamp in milliseconds

pythondatetimeunix-timestamp

提问by Tom

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

如何以毫秒为单位将格式20.12.2016 09:38:42,76为人类可读的时间转换为 Unix 时间戳?我发现了很多类似的问题,但没有找到这个特定的问题/答案。

回答by Milo

In Python 3 this can be done in 2 steps:

在 Python 3 中,这可以通过 2 个步骤完成:

  1. Convert timestring to datetimeobject
  2. Multiply the timestamp of the datetimeobject by 1000 to convert it to milliseconds.
  1. 将时间字符串转换为datetime对象
  2. datetime对象的时间戳乘以1000 以将其转换为毫秒。

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

strptimeaccepts your timestring and a format string as input. The timestring (first argument) specifies whatyou actually want to convert to a datetimeobject. The format string (second argument) specifies the actual formatof the string that you have passed.

strptime接受您的时间字符串和格式字符串作为输入。该TIMESTRING(第一个参数)指定什么你真的想转换为datetime对象。格式字符串(第二个参数)指定您传递的字符串的实际格式

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.
  • %d- 以零填充的十进制数表示的月份中的某一天。
  • %m- 以零填充的十进制数表示的月份。
  • %Y- 以世纪为十进制数的年份
  • %H- 小时(24 小时制)作为补零的十进制数。
  • %M- 分钟作为用零填充的十进制数。
  • %S- 第二个是用零填充的十进制数。
  • %f- 微秒作为十进制数,在左侧补零。

回答by Matthew Cox

For Python2.7 - modifying MYGz's answer to not strip milliseconds:

对于 Python2.7 - 修改 MYGz 的答案不去除毫秒:

from datetime import datetime

d = datetime.strptime("20.12.2016 09:38:42,76", "%d.%m.%Y %H:%M:%S,%f").strftime('%s.%f')
d_in_ms = int(float(d)*1000)
print(d_in_ms)

print(datetime.fromtimestamp(float(d)))

Output:

输出:

1482248322760
2016-12-20 09:38:42.760000

回答by Maurice Meyer

You need to parse your time format using strptime.

您需要使用strptime解析您的时间格式。

>>> import time
>>> from datetime import datetime
>>> ts, ms = '20.12.2016 09:38:42,76'.split(',')
>>> ts
'20.12.2016 09:38:42'
>>> ms
'76'
>>> dt = datetime.strptime(ts, '%d.%m.%Y %H:%M:%S')
>>> time.mktime(dt.timetuple())*1000 + int(ms)*10
1482223122760.0

回答by MYGz

For Python2.7

对于 Python2.7

You can format it into seconds and then multiply by 1000 to convert to millisecond.

您可以将其格式化为秒,然后乘以 1000 以转换为毫秒。

from datetime import datetime

d = datetime.strptime("20.12.2016 09:38:42,76", "%d.%m.%Y %H:%M:%S,%f").strftime('%s')
d_in_ms = int(d)*1000
print(d_in_ms)

print(datetime.fromtimestamp(float(d)))

Output:

输出:

1482206922000
2016-12-20 09:38:42

回答by Ivan Klass

For those who searches for an answer without parsing and loosing milliseconds, given dt_objis a datetime:

对于那些在不解析和丢失毫秒的情况下搜索答案的人,给出的dt_obj是一个日期时间:

python3 only, elegant

仅python3,优雅

int(dt_obj.timestamp() * 1000)

both python2 and python3 compatible:

python2和python3兼容:

import time

int(time.mktime(dt_obj.utctimetuple()) * 1000 + dt_obj.microsecond / 1000)