close

[Solved] AttributeError: ‘datetime.datetime’ object has no attribute ‘timestamp’

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error AttributeError: ‘datetime.datetime’ object has no attribute ‘timestamp’ in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How AttributeError: ‘datetime.datetime’ object has no attribute ‘timestamp’ Error Occurs?

Today I get the following error AttributeError: ‘datetime.datetime’ object has no attribute ‘timestamp’ in python.

How To Solve AttributeError: ‘datetime.datetime’ object has no attribute ‘timestamp’ Error ?

  1. How To Solve AttributeError: 'datetime.datetime' object has no attribute 'timestamp' Error ?

    To Solve AttributeError: 'datetime.datetime' object has no attribute 'timestamp' Error The timestamp method was added in Python 3.3. So if you're using Python 2.0, or even 2.7, you don't have it.

  2. AttributeError: 'datetime.datetime' object has no attribute 'timestamp'

    To Solve AttributeError: 'datetime.datetime' object has no attribute 'timestamp' Error The timestamp method was added in Python 3.3. So if you're using Python 2.0, or even 2.7, you don't have it.

Solution 1

As the other answers state, datetime.timestamp() was added on Python 3.3.

To get a similar behavior on Python < 3.3, you need to use time.mktime():

import time

def to_seconds(date):
    return time.mktime(date.timetuple())

And then, instead of calling start_date.timestamp(), you just call to_seconds(start_date)

Solution 2

The timestamp method was added in Python 3.3. So if you’re using Python 2.0, or even 2.7, you don’t have it.

There are backports of current datetime to older Python versions on PyPI, but none of them seems to be official, or up-to-date; you might want to try searching for yourself.

There are also a number of third-party replacement libraries that add functionality that isn’t in (2.x) datetime, including the ability to convert to Unix timestamps.


You can just copy the function out of the source code from 3.3 or later:

def timestamp(self):
    "Return POSIX timestamp as float"
    if self._tzinfo is None:
        s = self._mktime()
        return s + self.microsecond / 1e6
    else:
        return (self - _EPOCH).total_seconds()

… but you will have to modify things a bit to get them to work, because:

  • _EPOCH is deleted at the end of the module.
  • The 3.x _EPOCH is a tz-aware object built with a proper UTC timezone, which you don’t have in 2.x unless you’re using a third-party library like pytz.
  • The _mktime method and _tzinfo attribute don’t exist on 2.x datetime, so you need to simulate what they do as well.

If you don’t need the same function to work equally well for naive, GMT, and tz-aware datetimes, it won’t be that hard, but it’s still not quite trivial—and if you do need the full functionality, it’s going to be more painful.


Or it may be easier to port the equivalent code given in the docs.

For aware datetime instances:

(dt - datetime(1970, 1, 1, tzinfo=timezone.utc)).total_seconds()

Of course you still don’t have that timezone.utc, but for this purpose, you don’t need a full timezone object; you can use an instance of the example UTC class in the 2.x tzinfo docs.

… for naive:

timestamp = dt.replace(tzinfo=timezone.utc).timestamp()

… or:

timestamp = (dt - datetime(1970, 1, 1)) / timedelta(seconds=1)

Since you don’t have aware datetimes, that last one is all you need.


If your Python is old enough, timedelta may not have a __div__ method. In that case (if you haven’t found a backport), you have to do division manually as well, by calling total_seconds on each one, making sure at least one of them is a float, and dividing the numbers:

timestamp = ((dt - datetime(1970, 1, 1)).total_seconds() / 
    float(timedelta(seconds=1).total_seconds()))

But in this particular case, it should be pretty obvious that the divisor is just going to be 1.0, and dividing by 1.0 is the same as doing nothing, so:

timestamp = (dt - datetime(1970, 1, 1)).total_seconds()

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read