10

I'm struggling to add different amounts of seconds to a timestamp.

Let's suppose I want to add 1136 seconds to 2016-12-02 13:26:49. This is what I have thus far:

import datetime

if __name__ == '__main__':
    timestamp = datetime.datetime(year=2016, month=12, day=02, hour=13, minute=26, second=49)
    offset = 1140
    m, s = divmod(offset, 60)
    h, m = divmod(m, 60)

I saw in another post something similar to what I want, but that is not for Python.

Should I use datetime.datetime.combine()?

I have a big amount of data and I do not want to manually input the date for every sum.

Thank you in advance for any help.

Community
  • 1
  • 1
Annemie
  • 157
  • 1
  • 3
  • 13
  • 2
    https://docs.python.org/2/library/datetime.html#timedelta-objects –  Jan 09 '17 at 15:15

5 Answers5

12

You could use timedelta to add seconds to datetime object.

>>> import datetime
>>> now = datetime.datetime.now()
>>> now 
datetime.datetime(2017, 1, 9, 16, 16, 12, 257210)
>>> now + datetime.timedelta(seconds=1136)
datetime.datetime(2017, 1, 9, 16, 22, 12, 257210)
>>> 
Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47
9

Simply add a timedelta to the timestamp:

timestamp = datetime.datetime(year=2016, month=12, day=02, hour=13, minute=26, second=49)
d = datetime.timedelta(seconds=1136)
new_timestamp = timestamp+d

Running this in the console:

$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> timestamp = datetime.datetime(year=2016, month=12, day=02, hour=13, minute=26, second=49)
>>> d = datetime.timedelta(seconds=1136)
>>> new_timestamp = timestamp+d
>>> new_timestamp
datetime.datetime(2016, 12, 2, 13, 45, 45)

So the result is December 12, 2016 at 13:45:45.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
3

Add a timedelta:

>>> import datetime
>>> timestamp = datetime.datetime(year=2016, month=12, day=2, hour=13, minute=26, second=49)
>>> timestamp += datetime.timedelta(seconds=1136)
>>> timestamp
datetime.datetime(2016, 12, 2, 13, 45, 45)
Uriel
  • 15,579
  • 6
  • 25
  • 46
1

Use timedelta.

import datetime
from datetime import timedelta

timestamp = datetime.datetime(year=2016, month=12, day=02, hour=13, minute=26, second=49)

#offset = 1140
#m, s = divmod(offset, 60)
#h, m = divmod(m, 60)
extra = timedelta(seconds=1136)

print timestamp + extra
Joost
  • 480
  • 1
  • 5
  • 15
0

I know, there are already four answers but consider using the arrow module. It makes many date and time manipulations easier.

>>> import arrow
>>> arrow.get('2016-12-02 13:26:49').shift(seconds=+1136)
<Arrow [2016-12-02T13:45:45+00:00]>
>>> newTime = arrow.get('2016-12-02 13:26:49').shift(seconds=+1136)
>>> newTime.strftime('%d-%m-%y')
'02-12-16'

In the first statement after the import you can see that arrow can convert your timestamp to an internal time format and do the shift in one line of code.

In the next statement I save that result and show the it's easy to manipulate the internal format in the usual ways. (More are available with arrow.)

Bill Bell
  • 21,021
  • 5
  • 43
  • 58