Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add 30 minutes in time object in python 3?

I converted this string into an object:

import datetime
date_time_str = '2018-06-29 08:15:27.243860'
date_time_obj = datetime.datetime.strptime(date_time_str, '%Y-%m-%d %H:%M:%S.%f')

when I print the time object using this:

print(date_time_obj.time())

Now I need to add 30 minutes to the time. Can someone show me how ? I tried this with no luck

updated_time = date_time_obj.time()+ timedelta(minutes=30)
like image 865
Lulabelle95 Avatar asked Oct 20 '25 03:10

Lulabelle95


1 Answers

You should get rid of time() function since date_time_obj is already a valid datetime.datetime object (timedelta works with datetime.datetime, not datetime.time)

updated_time = date_time_obj + timedelta(minutes=30)
print(updated_time)

The above code would successfully give us datetime.datetime(2018, 6, 29, 8, 45, 27, 243860)

like image 74
Paul Lo Avatar answered Oct 21 '25 18:10

Paul Lo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!