1

I am supposed to add an extra second to the field end_time which is of type . I am not able to add an extra second to the time, for example the time is 10:34:45 and I want to make the time as 10:34:46 then using timedelta function I am not able to add the second nor can I convert this object to datetime.datetime object. The field is not a datetime.datetime field from the very start. The field is already a datetime.time object beforehand. So what is the best way to convert this datetime.time object to add the extra seconds.

print (end_time_n)
print (type(end_time_n))

Output:

12:17:04

<class 'datetime.time'>
davidism
  • 121,510
  • 29
  • 395
  • 339

3 Answers3

2

Using datetime module.

Ex:

import datetime
t = "10:34:45"
t = datetime.datetime.strptime(t, "%H:%M:%S")
print( (t + datetime.timedelta(seconds=1)).strftime("%H:%M:%S") )

Output:

10:34:46
Rakesh
  • 81,458
  • 17
  • 76
  • 113
1

You can do like this:

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

ref: https://stackoverflow.com/a/100345/7678093

Ali
  • 2,541
  • 2
  • 17
  • 31
1

Using datetime module and .timedelta function:

import datetime

x = datetime.timedelta(hours=10, minutes=20, seconds=30)
print (x) # returns 10:20:30

x = x + datetime.timedelta(seconds=1)
print (x) # returns 10:20:31
Lodi
  • 565
  • 4
  • 16