首页 > 解决方案 > 将时间戳(纳秒格式)转换为日期时间

问题描述

我有一个纳秒格式的时间戳,我想将其转换为日期时间,但出现错误

import datetime

example_timestamp= '1629617204525776950'

example_timestamp= int(example_timestamp)


timestamp_to_date_time = datetime.datetime.fromtimestamp(example_timestamp).strftime('%Y-%m-%d %H:%M:%S,%f')

标签: pythondatetimetimestamp

解决方案


您应该使用=分配值而不是:

你可以这样做。

import datetime
example_timestamp =  int('1629617204525776950')

timestamp_to_date_time = datetime.datetime.fromtimestamp(example_timestamp/1000000000).strftime('%Y-%m-%d %H:%M:%S,%f')
print(timestamp_to_date_time)
2021-08-22 07:26:44,525777

推荐阅读