python datetime remove minute and second information
now = datetime.now()
now2 = datetime(now.year,now.month,now.day,now.hour)
One option is to use timedelta
from datetime
:
import datetime
import numpy as np
def reset(dt):
return dt + datetime.timedelta(hours = 1, minutes = -dt.minute, seconds = -dt.second)
np.vectorize(reset)(peak_interval)
# array([datetime.datetime(2010, 12, 13, 7, 0),
# datetime.datetime(2011, 1, 12, 8, 0),
# datetime.datetime(2011, 3, 23, 17, 0),
# datetime.datetime(2011, 4, 19, 18, 0)], dtype=object)
You can easily modify just a few fields using datetime.datetime.replace
old_date = datetime.datetime(2011, 3, 23, 16, 45)
new_date = old_date.replace(minute=0, second=0, microsecond=0) + datetime.timedelta(hours=1)