当前位置:首页 > 技术问答 > Python报错AttributeError: 'datetime.datetime' 对象没有属性 'timestamp'

Python报错AttributeError: 'datetime.datetime' 对象没有属性 'timestamp'

技术问答 / 星之宇 / 2021-11-28 22:35 / 浏览:197 / 评论:0

问题描述

Python写程序测试已经通过了,但是在服务器上跑的时候报错了,出现以下报错:

root@SWL-NAS:/# /usr/bin/python /volume2/Share/work/test.py
Traceback (most recent call last):
  File "/volume2/Share/work/test.py", line 6, in <module>
    timestamp = nowtime.timestamp()
AttributeError: 'datetime.datetime' object has no attribute 'timestamp'以下

截取了一段代码如下:

# coding=utf-8
import time
from datetime import datetime

nowtime = datetime.now()
timestamp = nowtime.timestamp()
print(timestamp)


问题分析

Python 3.3之前是不支持datetime.timestamp() ,需要使用time.time()来获取。其他的时间可以使用time库的其他方式获取


解决方法

修改代码如下即可。

# coding=utf-8
import time
from datetime import datetime

timestamp = time.time()
print(timestamp)

目前有 0 条评论

    • 昵称
    • 邮箱
    • 网址