thileepan
thileepan

Reputation: 641

How to add missing seconds to a datetime object

I have a datetime object datetime.datetime(2017, 1, 13, 23, 50) The object doesn't have seconds field. How can I add zero (00) seconds to it so the object becomes datetime.datetime(2017, 1, 13, 23, 50, 00).

Upvotes: 2

Views: 2029

Answers (3)

L3viathan
L3viathan

Reputation: 27331

Maybe this is what you want:

>>> import datetime
>>> d = datetime.datetime(2017, 1, 13, 23, 50)
>>> print("datetime.datetime({d.year}, {d.month}, {d.day}, {d.hour}, {d.minute}, {d.second})".format(d=d))
datetime.datetime(2017, 1, 13, 23, 50, 0)

I still don't see why you would want this though.

Upvotes: 1

Gaurav
Gaurav

Reputation: 1109

You can try this

a = datetime.datetime(2017, 1, 13, 23, 50)
b = a + datetime.timedelta(seconds=00)

Upvotes: 1

match
match

Reputation: 11070

datetime only shows seconds in its output if seconds are not 0.

You can confirm this by doing:

x = datetime.datetime(2017, 1, 13, 23, 50)
x.second
# 0
x = x.replace(second=45)
# datetime.datetime(2017, 1, 13, 23, 50, 45)

Upvotes: 1

Related Questions