将时间戳(纳秒格式)转换为日期时间



我有一个纳秒格式的时间戳我想把它转换成datetime格式但得到错误

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')

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

你可以这样做。

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

最新更新