Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the hour with datetime.replace() in python

Given that foo is a valid datetime object in python,

One can change the hour represented in a datestamp (foo) by doing something something like:

foo2 = foo.replace( hour=5 ) 

Rather then replacing the hour with a particular value ( as is done above )..is it possible to increment the time in foo by say, 5 hours ? Something along the lines of:

foo2 = foo.replace( hour += 5 ) 

Which I know is not correct...but maybe that explains better what I am aiming to do...

I am limited to using python 2.5.1 ( the version on OS X 10.5.x) .. and am not able to add any modules such as pyTZ

like image 890
cit Avatar asked Feb 06 '10 15:02

cit


People also ask

How do you change date and time in Python?

The datetime() constructor in python accepts year, month and date values as parameters and create a datetime object. Pass the year, month and day values of the desired date object (as my_date. year, my_date. month, my_date.


1 Answers

That's what timedelta is for:

>>> import datetime
>>> d = datetime.datetime(2010, 12, 25, 18, 25)
>>> d + datetime.timedelta(hours = 8)
datetime.datetime(2010, 12, 26, 2, 25)
like image 105
balpha Avatar answered Oct 25 '22 05:10

balpha