What Is the Standard Way to Add N Seconds to Datetime.Time in Python

What is the standard way to add N seconds to datetime.time in Python?

You can use full datetime variables with timedelta, and by providing a dummy date then using time to just get the time value.

For example:

import datetime
a = datetime.datetime(100,1,1,11,34,59)
b = a + datetime.timedelta(0,3) # days, seconds, then other fields.
print(a.time())
print(b.time())

results in the two values, three seconds apart:

11:34:59
11:35:02

You could also opt for the more readable

b = a + datetime.timedelta(seconds=3)

if you're so inclined.


If you're after a function that can do this, you can look into using addSecs below:

import datetime

def addSecs(tm, secs):
fulldate = datetime.datetime(100, 1, 1, tm.hour, tm.minute, tm.second)
fulldate = fulldate + datetime.timedelta(seconds=secs)
return fulldate.time()

a = datetime.datetime.now().time()
b = addSecs(a, 300)
print(a)
print(b)

This outputs:

 09:11:55.775695
09:16:55

Adding seconds to datetime

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.

How to add seconds on a datetime value in Python?

Have you checked out timedeltas?

from datetime import datetime, timedelta
x = datetime.now() + timedelta(seconds=3)
x += timedelta(seconds=3)

how to add x number of hours to datetime.datetime() in python

Use datetime.timedelta:

import datetime

d = datetime.datetime(2021, 11, 19, 17, 6, 45)
t = datetime.timedelta(hours=8)

d+t

output: datetime.datetime(2021, 11, 20, 1, 6, 45)

Convert datetime.time in datetime.datetime

  1. Never perform calculations yourself when you can get the desired things from the standard library. The difference between two datetime.datetime objects gives you datetime.timedelta which already has a class attribute, seconds which you can return from your function.
  2. You can use datetime.combine to combine datetime.date and datetime.time.

Demo:

from datetime import datetime, date, time

def delta_seconds(end, origin):
return (end - origin).seconds

# Test
date = date(2021, 5, 3)
time = time(10, 20, 30)
origin = datetime.combine(date, time)
end = datetime.now()
print(delta_seconds(end, origin))

Output:

33213

Adding constant time onto datetime column

Use to_datetime for datetimes, then remove microseconds by Series.dt.floor and add 3 seconds:

# make date time object
df.timestamp = pd.to_datetime(df.timestamp)

# get the time second value of datetime
df["timestamp"] = df["timestamp"].dt.floor('s') + pd.Timedelta(seconds=3)

print (df)
timestamp value
0 2022-02-07 11:38:11 1.143380
1 2022-02-07 11:38:12 1.143410
2 2022-02-07 11:38:13 1.143400
3 2022-02-07 11:38:13 1.143433
4 2022-02-07 11:38:14 1.153433


Related Topics



Leave a reply



Submit