8

I have a date string like this and then use strptime() So its like this

my_time = datetime.datetime.strptime('07/05/15', '%m/%d/%Y')

and now I want to add 23 hours and 59 minutes to my_time

I have tried .timedelta but it doesn't work? How could I do this?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
spen123
  • 3,464
  • 11
  • 39
  • 52

3 Answers3

14

Add time afterwards; you can do so with the datetime.replace() method to produce a new datetime object:

my_time = datetime.datetime.strptime('07/05/15', '%m/%d/%y')
my_time = my_time.replace(hour=23, minute=59)

The datetime.strptime() sets the hour and minute values to the default, 0. Note that for a two-digit year (like 15) you'd use %y, not %Y, which is for a four-digit year.

You could also use the datetime.combine() class method to pair up a date and a time object:

my_time = datetime.datetime.strptime('07/05/15', '%m/%d/%y')
my_time = datetime.datetime.combine(my_time.date(), datetime.time(23, 59))

If you feel you must use a timedelta(), take into account that adding it will again produce a new datetime object. You could use augmented assignment to add it 'in-place':

my_time = datetime.datetime.strptime('07/05/15', '%m/%d/%y')
my_time += datetime.timedelta(hours=23, minutes=59)

Demo:

>>> import datetime
>>> my_time = datetime.datetime.strptime('07/05/15', '%m/%d/%y')
>>> my_time.replace(hour=23, minute=59)
datetime.datetime(2015, 7, 5, 23, 59)
>>> datetime.datetime.combine(my_time.date(), datetime.time(23, 59))
datetime.datetime(2015, 7, 5, 23, 59)
>>> my_time + datetime.timedelta(hours=23, minutes=59)
datetime.datetime(2015, 7, 5, 23, 59)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • if you need to add an amount that is negative or more than one day then only the `timedelta()` solution would work. – jfs Aug 07 '15 at 20:32
10

Firstly, based on the date string you are providing, the format seems to be wrong , you should use %y (small y) for 2 digit years, %Y (capital Y) is for 4 digit years.

Then you can add time to my_time using timedelta as follows, but the addition operation produces a new datetime object, does not change the my_time in place.

Hence, you will need to assign it back to your my_time like this -

>>> import datetime

>>> my_time = datetime.datetime.strptime('07/05/15', '%m/%d/%y')
>>> my_time = my_time + datetime.timedelta(hours=23,minutes=59)
>>> my_time
datetime.datetime(2015, 7, 5, 23, 59)
Sharon Dwilif K
  • 1,218
  • 9
  • 17
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
  • i dont know why but it throws error ```AttributeError: type object 'datetime.datetime' has no attribute 'timedelta'```. My code looks like ```from datetime import datetime; newTime = passedTime + datetime.timedelta()``` – Alexey Nikonov Oct 20 '19 at 17:29
  • this answer helped me to solve that issue https://stackoverflow.com/a/12906456/9277453 – Alexey Nikonov Oct 20 '19 at 17:39
  • If anyone has the same error that Alexey Nikonov had: You need to do `import datetime; newTime = passedTime + datetime.timedelta()`. Alexey essentially did `import datetime; newTime = passedTime + datetime.datetime.timedelta()` because of how he structured his import as `from datetime import datetime`. – FlamePrinz Jan 23 '21 at 21:38
0

pandas way:

import pandas as pd 
df['time'] = pd.to_datetime(ddf['time']) + pd.Timedelta('16:00:00')
Jerald Achaibar
  • 407
  • 5
  • 9