将unix时间戳以毫秒为单位转换为UTC时间&日期格式化python3

时间:2018-06-17 01:50:49

标签: python python-3.x unix unix-timestamp

我有一个Unix时间戳列表,我希望将其转换为UTC时间:

times = [..., "1371605102000", "1375245962000", ...]

但是,这些时间以毫秒为单位。我试过这段代码:

from datetime import datetime
import tzlocal

unix_timestamp = float("1371605102000")
local_timezone = tzlocal.get_localzone()
local_time = datetime.fromtimestamp(unix_timestamp, local_timezone)

print(local_time.strftime("%B %d %Y")) 

但它抛出了这个错误:

Traceback (most recent call last):
  File "temp.py", line 6, in <module>
    local_time = datetime.fromtimestamp(unix_timestamp, local_timezone)
ValueError: year 45434 is out of range

我觉得这与时间戳以毫秒为单位的事实有关,但我并不完全确定。

除此之外,我还想知道是否有办法将日期格式化为这样:

...
Jan 04, 2017
Oct 27, 2016
...

而不是:

...
January 4, 2017
October 27, 2016
....

0 个答案:

没有答案